Javascript下一个和前一天的功能 [英] Javascript next and previous day function

查看:89
本文介绍了Javascript下一个和前一天的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发日历,需要遍历下一天和前一天。我怎么用javascript写这个?进入下一个/上个月时,以下是否会正确计算?喜欢C#的DateTime.Today.AddDays(1)会吗?

I am developing a daily calendar and need to traverse to the next and previous day. How would I write this in javascript? Will the following calculate correctly when going to the next / previous month? Like C#'s DateTime.Today.AddDays(1) will?

new Date(year, month, day + 1)

我担心的是,如果我在3月31日执行此操作,它将计算3月32日...这不会很明显。

My concern is that if I execute this on March 31st, it will calculate March 32nd...which wouldn't work obviously.

如果有人可以提供这样做的功能,那就太棒了!

If someone could provide a function to do both that would be great!

提前致谢!

推荐答案

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

这篇关于Javascript下一个和前一天的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆