Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
120
rated 0 times [  127] [ 7]  / answers: 1 / hits: 38444  / 12 Years ago, fri, august 3, 2012, 12:00:00

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.


More From » date

 Answers
22

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;

[#83883] Thursday, August 2, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aricl

Total Points: 215
Total Questions: 91
Total Answers: 94

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;