I'm using the phonegap localNotifications plugin which specifies that I can set a notification to occur weekly.
However, it appears the javascript date object only has .getDay()
and not .setDay()
.
I've got an json object of the days of the week I want to set to repeat,
set_days = {"mon":false, "tues":true,"wed":false,"thurs":true...}
how do you set a day in javascript? Because it is setting a notification, the day has to be in the future, so I don't want to get the most recent "wednesday", but only the next "wednesday".
here's a link to the plugin, but I don't think this is really specific to the plugin.
getDay() The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.
The getDay() method returns an integer between 0 and 6 that represents the day of the week for the given date: 0 is Sunday, 1 is Monday, 2 is Tuesday, etc. The day of the month for the first day of the week is equal to day of the month - day of the week .
Translate the Date(). getDay() to a string day name : getDay « Date « JavaScript Tutorial. The getDay() method returns the day of the week expressed as an integer from 0 (Sunday) to 6 (Saturday). 12.6.
how do you set a day in javascript?
You mean, in the current week? When does a week start?
Assuming it starts on Sunday (as in getDay
: 0 - Sunday, 1 - Monday, etc), this will work:
var date, daytoset; // given: a Date object and a integer representing the week day
var currentDay = date.getDay();
var distance = daytoset - currentDay;
date.setDate(date.getDate() + distance);
To set the date to a weekday in the next 7 days, use this:
var distance = (daytoset + 7 - currentDay) % 7;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With