Javascript 在javascript中设置星期几
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11789647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting day of week in javascript
提问by pedalpete
I'm using the phonegap localNotifications plugin which specifies that I can set a notification to occur weekly.
我正在使用 phonegap localNotifications 插件,它指定我可以设置每周发生一次通知。
However, it appears the javascript date object only has .getDay()
and not .setDay()
.
但是,似乎 javascript 日期对象只有.getDay()
而没有.setDay()
.
I've got an json object of the days of the week I want to set to repeat,
我有一个我想设置为重复的一周中的几天的 json 对象,
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".
你如何在javascript中设置一天?因为是设置通知,那一天必须在未来,所以我不想得到最近的“星期三”,而只想得到下一个“星期三”。
here's a link to the plugin, but I don't think this is really specific to the plugin.
这是插件的链接,但我认为这不是插件特有的。
回答by Bergi
how do you set a day in javascript?
你如何在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:
假设它从星期日开始(如getDay
:0 - 星期日、1 - 星期一等),这将起作用:
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:
要将日期设置为接下来 7 天的工作日,请使用以下命令:
var distance = (daytoset + 7 - currentDay) % 7;
回答by Arthur
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:
我不擅长更新像 Date 这样的 JS 对象,所以我没有提供完整的解决方案来实现,Date.proptype.setDay
但想法在这里:
// 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'Twant to loose ref and update the date
you send as arg you can replace this line:
如果你真的不要想松ref和更新date
您发送的精氨酸可以替换此行:
date = date ? new Date(date) : new Date(); // Loose ref if exist, or take now
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
date = date ? date : new Date(); // KEEP ref if exist in order to update original, or take now
回答by Preston
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.
日期对象确实有一个 set 方法。
http://www.tutorialspoint.com/javascript/javascript_date_object.htm
http://www.tutorialspoint.com/javascript/javascript_date_object.htm