简体   繁体   English

获取一周中特定日期的下一个日期

[英]Getting the next date for a specific day of the week

Given a start date of 2014-07-08 (Tuesday) I would want to perform a check to find the closest day of the week. 给定开始日期为2014-07-08(星期二),我想执行一次检查以查找一周中最接近的一天。

For example, I need to be able to perform the following calls: 例如,我需要能够执行以下调用:

  • First Monday (should return 2014-07-14) 第一个星期一(应于2014-07-14返回)
  • First Wednesday (should return 2014-07-09) 第一个星期三(应返回2014-07-09)
  • First Saturday (should return 2014-07-12) 第一个星期六(应于2014-07-12返回)
  • Etc. 等等。

I know moment.js lets you do something like 我知道moment.js可以让您做类似的事情

moment("2014-07-08").day(1)

To get the date of Monday this week, however I need to know if the DOW is before/after the current date and apply the offset accordingly; 要获取本周星期一的日期,但是我需要知道DOW是否在当前日期之前/之后,并相应地应用偏移量。 if that makes any sense.. 如果那有意义的话..

Any thoughts? 有什么想法吗?

Just add 7 if the day of the week you are looking for is or was before the current day of the week: 如果您要查找的是星期几,则添加7

var date = moment("2014-07-08");
var dow = date.day();
var nextX = date.day(X + dow <= X ? 7 : 0);

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

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