本文介绍了在javascript中设置星期几的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用phonegap localNotifications插件,该插件指定我可以每周发送通知。

I'm using the phonegap localNotifications plugin which specifies that I can set a notification to occur weekly.

但是,似乎javascript日期对象只有 .getDay()而不是 .setDay()

However, it appears the javascript date object only has .getDay() and not .setDay().

我有一个json对象,我想设置一周的日子,

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...}

如何在JavaScript中设置一天?因为它正在设置一个通知,那一天必须在将来,所以我不想得到最近的星期三,但只有下一个星期三。

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.

推荐答案

你的意思是在本周?一周开始?

You mean, in the current week? When does a week start?

假设它从星期天开始(如:0 - 星期日,1 - 星期一等),这将工作:

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);






将日期设置为下一个工作日7天,使用这个:


To set the date to a weekday in the next 7 days, use this:

var distance = (daytoset + 7 - currentDay) % 7;

这篇关于在javascript中设置星期几的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:31