简体   繁体   English

获取JavaScript中某一天的下周日期

[英]Get next week's date of a certain day in JavaScript

Depending on today's date (new Date()), I would like to get the date of the next Thursday at 7pm in javascript. 根据今天的日期(新的日期()),我想在javascript中获取下周四晚上7点的日期。 For example: 例如:

If today's date is "Mon Apr 24 2017 13:00:00 GMT" I am looking for the result: 如果今天的日期是“2017年4月24日13:00:00 GMT”,我正在寻找结果:

Thu Apr 27 2017 19:00:00 GMT

However, if today's date is "Thu Apr 27 2017 21:00:00 GMT" (a Thursday, but past 7pm) I am looking for the result: 但是,如果今天的日期是“2017年4月27日星期四21:00:00格林尼治标准时间”(星期四,但是晚上7点),我正在寻找结果:

Thu May 4 2017 19:00:00 GMT

Any help would be much appreciated! 任何帮助将非常感激!

Okay. 好的。 So if moment.js is not available, work with the milliseconds. 因此,如果moment.js不可用,请使用毫秒。

var d = new Date();
var n = d.getTime(); // n == time in milliseconds since 1st Jan 1970
var weekday = d.GetDay() // 0...6 ; 0 == Sunday, 6 = Saturday, 4 = Thursday
var numDaysToNextThursday = weekday >= 4 ? 7 - (weekday-4) : 4 - weekday;
var nextThursday_msecs = n + numDaysToNextThursday * 24 * 60 * 60 * 1000;
var theDate = new Date(nextThursday_msecs); // this is the date

Edit: I can see that 7PM on Thursday is the magic moment. 编辑:我可以看到星期四晚上7点是神奇的时刻。 The above is not quite right in that regard; 在这方面,上述情况并不完全正确; it will set theDate to be 7 days ahead if we are currently at Thursday regardless of actual time. 如果我们目前在星期四,无论实际时间如何,它都会将日期提前7天。 You will probably still get the idea anyway. 无论如何,你可能仍会得到这个想法。

Maybe something like the following (you can extend it if you want a more specific time than just hour and minute): 也许类似下面的内容(如果你想要比一小时和一分钟更具体的时间,你可以扩展它):

 // day: 0=Sunday, 1=Monday...4=Thursday... function nextDayAndTime(dayOfWeek, hour, minute) { var now = new Date() var result = new Date( now.getFullYear(), now.getMonth(), now.getDate() + (7 + dayOfWeek - now.getDay()) % 7, hour, minute) if (result < now) result.setDate(result.getDate() + 7) return result } console.log(nextDayAndTime(4, 19, 0).toString()) // Thursday 7pm console.log(nextDayAndTime(0, 19, 0).toString()) // Sunday 7pm console.log(nextDayAndTime(1, 19, 0).toString()) // Monday 7pm (later today as of now in my timezone) console.log(nextDayAndTime(1, 7, 30).toString()) // Monday 7:30am (next week, in my timezone) console.log(nextDayAndTime(2, 19, 0).toString()) // Tuesday 7pm 

The two key bits are now.getDate() + (7 + dayOfWeek - now.getDay()) % 7 , which figures out the next Thursday (or specified day) starting from today (that's what the % 7 does, and then the if (result < now) that double-checks if the required time has passed yet today and if so adds a week. 两个关键位是now.getDate() + (7 + dayOfWeek - now.getDay()) % 7 ,它从今天开始计算下一个星期四(或指定日期)(这是% 7作用,然后是if (result < now)重复检查所需时间是否已经过去,如果是这样,则增加一周。

You can easly get next thursday by simple recounting. 您可以通过简单的重新计算轻松获得下一个星期四。

7 - number of days in a week 4 - index of thursday in a week calculation - you need to subtract index of current day of a week to get thursday 7 - 一周中的天数4 - 一周中星期四的指数计算 - 你需要减去星期几当天的指数以获得星期四

 var day = new Date(); var days = 7 - day.getDay() + 4; var nextDay = new Date(day.setDate(day.getDate() + days)); console.log(nextDay.toString()); 

codepen to check other days https://codepen.io/kejt/pen/LyRGBX?editors=1111 codepen检查其他日子https://codepen.io/kejt/pen/LyRGBX?editors=1111

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM