Reputation: 9289
i have a requirement where i only get the name of the day. like what i am doing on friday.
i want to determine if the asked day is beyond 2 days. for example, like today is tuesday. and if someone ask what i am doing on friday. then friday is the 3rd day from today.
in javascript i could see i can do data operation like
var tomorrow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
schDate = tomorrow.toDateString();
but how do i find if the asked by just day name is beyond 2 days or not?
Upvotes: 1
Views: 71
Reputation: 862
new Date().getDay();
The getDay
function will return the number of the day.
Sunday is 0, Monday is 1, and so on.
That means Wednesday = 3
and Friday = 5
however when it is Saturday = 6
the next 2 days will be 0 and 1 .
Upvotes: 0
Reputation: 14423
If you only get day name:
let days = {
'sunday' : 0,
'monday' : 1,
'tuesday' : 2,
'wednesday' : 3,
'thursday' : 4,
'friday' : 5,
'saturday' : 6,
}
function distanceDays(from, to){
let date = to;
if(date < from) date += 7;
return Math.abs(today - date);
}
let date = days['sunday'];
let today = (new Date).getDay();
console.log(distanceDays(today, date));
Upvotes: 0
Reputation: 1418
var daysArr = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var today = new Date().getDay();
//return if day difference is bigger than 2
function dayDiff(day){
if(Math.abs(daysArr.indexOf(day) - today) > 2){
return true;
}else{
return false;
}
}
dayDiff("Wednesday");
Upvotes: 1