javascript Moment.js 使用夏令时添加天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21318357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moment.js issue adding days with Daylight Savings Time
提问by earnaz
In Moment.js I have the next problem:
在 Moment.js 我有下一个问题:
1.I create a moment date:
1.我创建了一个时刻日期:
var m = moment(new Date(2014, 9, 18, 0, 0, 0));
2.If I call toString function:
2.如果我调用toString函数:
m.toString() //"Sat Oct 18 2014 00:00:00 GMT-0300"
3.Now, I add one day I have the next output:
3.现在,我添加一天我有下一个输出:
m.add("days",1).toString() //"Sat Oct 18 2014 00:00:00 GMT-0300"
What I get 18 again?Momentjs should change the date.
什么我又得到18?Momentjs 应该更改日期。
EDIT: Issue in Chrome 32.0.1700.76 m
编辑:Chrome 32.0.1700.76 m 中的问题
EDIT2: MomentJs version 2.5.1
EDIT2:MomentJs 版本 2.5.1
EDIT3: Timezone UTC-3
EDIT3:时区 UTC-3
回答by Nathaniel Johnson
I have looked at you code and at first I did not get the same results. However when I changed the timezone to Brazil (GMT-03:00) - Sao Paulo I got the same result. This is clearly a bug and has now been traced to V8 and reported.
我看过你的代码,起初我没有得到相同的结果。但是,当我将时区更改为巴西 (GMT-03:00) - 圣保罗时,我得到了相同的结果。这显然是一个错误,现在已经追溯到 V8 并报告了。
var m = moment(new Date(2014, 9, 18, 0, 0, 0));
console.log(m.toString());
console.log(m.add("days",1).toString());
Sat Oct 18 2014 00:00:00 GMT-0300 script.js:4
Sat Oct 18 2014 00:00:00 GMT-0300 script.js:5
2014 年 10 月 18 日星期六 00:00:00 GMT-0300 script.js:4
2014 年 10 月 18 日星期六 00:00:00 GMT-0300 script.js:5
I have submitted a bug: https://github.com/moment/moment/issues/1440
我提交了一个错误:https: //github.com/moment/moment/issues/1440
Update
更新
Moment.js is not responsible for this bug. It has been tracked to a bug in V8 (the javascript engine used by both Chrome and Node). I have filed a bug with V8 that you can follow here: https://code.google.com/p/v8/issues/detail?id=3116
Moment.js 不对这个错误负责。它已被追踪到 V8(Chrome 和 Node 使用的 javascript 引擎)中的一个错误。我已经提交了一个关于 V8 的错误,你可以在这里关注:https: //code.google.com/p/v8/issues/detail?id=3116
Here is the work that Isaac Cambron did to track it down.
这是 Isaac Cambron 为追踪它所做的工作。
OK, reproduced now in both Ubuntu and OSX (I was using a different Brazilian city in my tests before). I'm using Node, not Chrome, but for our purposes V8 is V8.
好的,现在在 Ubuntu 和 OSX 中复制(我之前在测试中使用了不同的巴西城市)。我使用的是 Node,而不是 Chrome,但就我们而言,V8 是 V8。
moment([2014, 9, 18]).add(1, 'd').format(); //=> '2014-10-18T00:00:00-03:00'
The problem is that Moment essentially does this:
问题在于 Moment 本质上是这样做的:
var d = new Date(2014, 9, 18);
d.setDate(19);
d.toString(); // => Sat Oct 18 2014 23:00:00 GMT-0300 (BRT)
//wtf?
Then it sets the hours to zero. Since V8 weirdly sets the time to late on Oct 18 even though we specifically asked it to set it to Oct 19, the answer comes out wrong. This is all especially weird because the DST transition here is a jump forward, meaning if anything it should end up 1:00, not 23:00 the previous day.
然后它将小时数设置为零。由于 V8 奇怪地将时间设置为 10 月 18 日晚,即使我们特别要求它设置为 10 月 19 日,答案是错误的。这一切都特别奇怪,因为这里的 DST 过渡是向前跳跃,这意味着它应该在前一天的 1:00 结束,而不是 23:00。
In fact, it even does this:
事实上,它甚至这样做:
new Date("October 18, 2014"); //=> Sat Oct 18 2014 00:00:00 GMT-0300 (BRT)
new Date("October 19, 2014"); //=> Sat Oct 18 2014 23:00:00 GMT-0300 (BRT)
回答by Jason M. Batchelor
According to this jsFiddle, what you're doing /should/ be working. What browser are you testing in?
根据这个jsFiddle,你在做什么/应该/正在工作。你在什么浏览器中测试?
http://jsfiddle.net/mori57/Nq3KD/
http://jsfiddle.net/mori57/Nq3KD/
var m = moment(new Date(2014, 9, 18, 0, 0, 0));
console.log(m.toString()); // Firebug output: Sat Oct 18 2014 00:00:00 GMT-0400
console.log(m.add("days",1).toString()); // output: Sun Oct 19 2014 00:00:00 GMT-0400
回答by Akalya
Simply you can use the below code to get next date in moment.js
只需使用以下代码即可获取下一个日期 moment.js
var date='2014/09/18';
var nextDate = moment(date, 'YYYY/MM/DD').add('days', 1).format('YYYY/MM/DD');
console.log(nextDate); // 2014/09/19
For more details , look at below link
有关更多详细信息,请查看以下链接
回答by Jan
This is a rather old post but I got here via Google with the same issue. What I ended up using was the following code, maybe this is helpful for someone else:
这是一个相当古老的帖子,但我通过谷歌来到这里,遇到了同样的问题。我最终使用的是以下代码,也许这对其他人有帮助:
var then = moment("23.07.2014", "DD.MM.YY");
var before = moment.unix(then.unix()-86400); // 86400 seconds/day
var after = moment.unix(then.unix()+86400);
回答by Memphis335
You should pass the number first and then the "days"
你应该先传递数字,然后是“天”
var m = moment(new Date(2014, 9, 18, 0, 0, 0));
console.log(m.toString());
console.log(m.add(1, "days").toString());
Tested in Chrome Version 50.0.2661.37 beta-m (64-bit)
在 Chrome 版本 50.0.2661.37 beta-m(64 位)中测试
回答by jakub.g
General remark to avoid issues like this (as you can see, they dohappen, and are sometimes happening due to a browser bug): if you represent a dayusing Date
object, you can just have a convention to pass noon(12h00) instead of midnight (00h00) to the Date
constructor, and then in the function that prepares date for display, cut it off.
避免此类问题的一般性评论(如您所见,它们确实发生,并且有时由于浏览器错误而发生):如果您使用对象表示一天Date
,则可以使用约定来传递中午(12h00) 而不是午夜(00h00)到Date
构造函数,然后在准备显示日期的函数中,将其切断。
The bugs in browsers, and issues due to DST usually shift the time by one hour (time goes back from 00h00 to 23h00 the previous day). If you just use midday, it should not happen (in case of similar bugs, time will go back from 12h00 to 11h00 for instance, but the day will not change).
浏览器中的错误以及 DST 引起的问题通常会将时间移动一小时(时间从前一天的 00:00 回溯到 23:00)。如果你只是使用中午,应该不会发生(如果出现类似的错误,时间会从12:00回到11:00,但日期不会改变)。
(Of course you need to remember that the date is noon when you pass it around, sometimes it might not be good)
(当然你要记住,当你传递它时,日期是中午,有时可能不太好)
For instance, have a look at this bug in YUI library which we were using in on of our company apps:
例如,看看我们在公司应用程序中使用的 YUI 库中的这个错误:
https://github.com/yui/yui2/pull/15
https://github.com/yui/yui2/pull/15
This happened only in Firefox, only on Windows, only in particular timezone, and only in particular month. But it did happen.
这只发生在 Firefox 中,仅在 Windows 上,仅在特定时区,仅在特定月份发生。但它确实发生了。