Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting day of week in javascript

Tags:

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.

like image 608
pedalpete Avatar asked Aug 03 '12 05:08

pedalpete


People also ask

How do I get the day of the week in JavaScript?

getDay() The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.

What is the first day of the week in JavaScript?

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 .

How do you get day from string?

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.


1 Answers

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;
like image 187
Bergi Avatar answered Oct 23 '22 13:10

Bergi