简体   繁体   English

Javascript第二天和前一天的功能

[英]Javascript next and previous day function

I am developing a daily calendar and need to traverse to the next and previous day. 我正在开发每日日历,需要遍历下一天和前一天。 How would I write this in javascript? 我该如何用JavaScript编写? Will the following calculate correctly when going to the next / previous month? 下个月/上个月时,以下内容会正确计算吗? Like C#'s DateTime.Today.AddDays(1) will? 像C#的DateTime.Today.AddDays(1)一样吗?

new Date(year, month, day + 1)

My concern is that if I execute this on March 31st, it will calculate March 32nd...which wouldn't work obviously. 我担心的是,如果我在3月31日执行此操作,它将计算3月32日...这显然不起作用。

If someone could provide a function to do both that would be great! 如果有人可以提供同时执行这两个功能的功能,那就太好了!

Thanks in advance! 提前致谢!

var dateString = '30 Apr 2010'; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate()+1); // create new increased date

You don't need to worry. 不用担心 It won't. 不会的

date = new Date(2012, 2, 31 + 1);
console.log(date);

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

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