pedalpete
pedalpete

Reputation: 21556

setting day of week in javascript

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.

Upvotes: 32

Views: 42738

Answers (3)

Arthur
Arthur

Reputation: 5158

This is an answer already well completed but I found a solution that I want to share with you.

I'm not adept of updating JS object like Date, so I did not provide a complete solution to implement Date.proptype.setDay but the idea is here:

// Get a day of the current week
// Example "Get Monday" : getWeekDay({day:1})

// Args:
//  - day {Number} Same format that Date.getDay() > Sunday - Saturday : 0 - 6
//  - inWeeks {Number} Number of week relative to current, -1: Previous week, 1: Next week, 2: in 2 weeks
//  - date {Date} Use this date instead of now to get relative current week
//  - atMidnight {Bool} Force result date to be at 00:00:00:00 (instead of keeping current/date time)
/*export*/ const getWeekDay = ({
  day, inWeeks=0,
  date,
  atMidnight=false, // Force days to be at 00:00:00:00
}={}) => {
  date = date ? new Date(date) : new Date(); // Loose ref if exist, or take now
  day = (typeof day !== 'number' && typeof day !== 'string')
    ? date.getDay()
    : day;
  date.setDate(date.getDate() + inWeeks * 7 + (day - date.getDay()));
  if (atMidnight) {
    date.setHours(0,0,0,0);
  }
  return date;
};

// Same that getWeekDay
// but return an array of all days in the week [Sunday, Monday, .. Saturday]
// Instead of one single day

// Additional Args:
// - days: Array of day: {Number} Same format that Date.getDay() > Sunday - Saturday : 0 - 6
 /*export*/ const getWeekDays = ({
  days = [ ...Array(7).keys() ], 
  ...args
}) => days.map(day => getWeekDay({...args, day}));

// Demo: 

let wed = getWeekDay({inWeek:1, day:3})
console.log("The next Wednesday is: " + wed);
console.log("The next one (at midgnight): " + getWeekDay({date: wed, inWeeks:1, atMidnight:true}));
console.log("All days 2 weeks ago: " + getWeekDays({inWeeks:-2}));
console.log("Only Monday and Wednesday next week: " + getWeekDays({inWeeks:1, days:[1,3]}));

Additional Note

If you really DON'T want to loose ref and update the date you send as arg you can replace this line:

date = date ? new Date(date) : new Date(); // Loose ref if exist, or take now

by:

date = date ? date : new Date(); // KEEP ref if exist in order to update original, or take now

Upvotes: 1

Bergi
Bergi

Reputation: 664969

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;

Upvotes: 76

Preston
Preston

Reputation: 19

Are you creating a new date object to set on?

var d = new Date();
d.setDate(11);

would result in

Tue Aug 14 2012 00:31:56 GMT-0500 (Central Daylight Time)

The date object does have a set method.

http://www.tutorialspoint.com/javascript/javascript_date_object.htm

Upvotes: -3

Related Questions