在JavaScript中增加日期 [英] Incrementing a date in JavaScript

查看:165
本文介绍了在JavaScript中增加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在JavaScript中增加一天的日期值。



例如,我有一个日期值2010-09-11,我需要存储日期在JavaScript变量中。



如何按日增加日期?

解决方案

您的三个选择:



1。使用JavaScript的日期对象(无库):



我之前对#1的答案是错误的(它添加了24小时,未能考虑到夏令时间的过渡; 聪明的人类指出,将在11月失败7,2010年在东部时区)。相反, Jigar的回答是没有库的正确方法: / p>

  var tomorrow = new Date 
tomorrow.setDate(tomorrow.getDate()+ 1);

即使在一个月(或一年)的最后一天也是如此,因为JavaScript日期对象是关于翻转的智能:



  var lastDayOf2015 = new Date 2015年11月31日); snippet.log(2015年最后一天:+ lastDayOf2015.toISOString()); var nextDay = new Date(lastDayOf2015); var dateValue = nextDay.getDate()+ 1; snippet.log 将date日期设置为+ dateValue); nextDay.setDate(dateValue); snippet.log(生成日期:+ nextDay.toISOString());  

 <! - 脚本提供`snippet`对象,请参见http://meta.stackexchange.com/a / 242144/134069  - >< script src =// tjcrowder.github.io/simple-snippets-console/snippet.js\"></script> 



(此答案目前被接受,所以我无法删除。在被接受之前,我建议OP接受Jigar,但也许他们接受了这个项目#2或#3在列表中。)



2。使用 MomentJS



  var tomorrow = moment(今).add(1,'day'); 

(当心添加修改实例调用它,而不是返回一个新的实例,所以 today.add('days',1)将修改今天这就是为什么我们从 var tomorrow = ... 开始克隆操作。)



3。使用 DateJS ,但尚未在很长时间内更新:



  var today = new Date(); //或Date.today()
var tomorrow = today.add(1).day();


I need to increment a date value by one day in JavaScript.

For example, I have a date value 2010-09-11 and I need to store the date of the next day in a JavaScript variable.

How can I increment a date by a day?

解决方案

Three options for you:

1. Using just JavaScript's Date object (no libraries):

My previous answer for #1 was wrong (it added 24 hours, failing to account for transitions to and from daylight saving time; Clever Human pointed out that it would fail with November 7, 2010 in the Eastern timezone). Instead, Jigar's answer is the correct way to do this without a library:

var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);

This works even for the last day of a month (or year), because the JavaScript date object is smart about rollover:

var lastDayOf2015 = new Date(2015, 11, 31);
snippet.log("Last day of 2015: " + lastDayOf2015.toISOString());
var nextDay = new Date(lastDayOf2015);
var dateValue = nextDay.getDate() + 1;
snippet.log("Setting the 'date' part to " + dateValue);
nextDay.setDate(dateValue);
snippet.log("Resulting date: " + nextDay.toISOString());

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

(This answer is currently accepted, so I can't delete it. Before it was accepted I suggested to the OP they accept Jigar's, but perhaps they accepted this one for items #2 or #3 on the list.)

2. Using MomentJS:

var today = moment();
var tomorrow = moment(today).add(1, 'day');

(Beware that add modifies the instance you call it on, rather than returning a new instance, so today.add('days', 1) would modify today. That's why we start with a cloning op on var tomorrow = ....)

3. Using DateJS, but it hasn't been updated in a long time:

var today = new Date(); // Or Date.today()
var tomorrow = today.add(1).day();

这篇关于在JavaScript中增加日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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