[英]Dates display behind by a day
I'm encountering some weird behavior while using Moment.js. 使用Moment.js时遇到一些奇怪的行为。 I have some helper classes attached to the Date prototype, which are apparently are causing each date to display behind by a day.
我在Date原型上附加了一些辅助类,这些辅助类显然导致每个日期都落后一天。
Date.prototype.format = function(){
return moment(this).format('MM/DD/YYYY')
}
var date = new Date('2008-05-13T00:00:00')
date.format() // => 05/12/2008, but should be 05/13/2008
A couple of other weird things I've noticed: 我注意到了其他一些奇怪的事情:
date.getDate() // => yields 12, but should be 13
But, if I instantiated the Moment object directly with the UTC string, then it works: 但是,如果我直接使用UTC字符串实例化Moment对象,那么它将起作用:
moment('2008-05-13T00:00:00').format('MM/DD/YY') // => 05/13/08
But I'm dealing with plain date objects, and modifying every date to a Moment object isn't my favorite idea. 但是我正在处理普通日期对象,将每个日期修改为Moment对象并不是我的最爱。 I have already tried modifying the
format
function to extract the UTC string from the date and see if it displays correctly then, but to no avail. 我已经尝试过修改
format
函数以从日期中提取UTC字符串,然后查看它是否正确显示,但无济于事。
date.toUTCString() // => Correctly yields "Tue, 13 May 2008 00:00:00 GMT"
moment(date.toUTCString()).format('MM/DD/YY') // => still 05/12/08
Any ideas what's happening here? 有什么想法吗? Is there a problem with the date constructor?
日期构造函数有问题吗?
EDIT: Outputting the time as well: 编辑:也输出时间:
moment(date).format('MM/DD/YY hh:mm:ss') // => "05/12/08 08:00:00"
You have to tell moment.js that you to display the date in UTC: 您必须告诉moment.js您以UTC显示日期:
moment(this).utc().format('MM/DD/YYYY')
More in the documentation: http://momentjs.com/docs/#/parsing/utc/ 文档中的更多信息: http : //momentjs.com/docs/#/parsing/utc/
But, if I instantiated the Moment object directly with the UTC string, then it works:
但是,如果我直接使用UTC字符串实例化Moment对象,那么它将起作用:
moment('2008-05-13T00:00:00').format('MM/DD/YY') // => 05/13/08`
Moment.js interprets the argument as local time : Moment.js 将参数解释为本地时间 :
By default, moment parses and displays in local time.
默认情况下,moment解析并以本地时间显示。
Whereas new Date()
(and Date.parse
) interpret the value as UTC time : 而
new Date()
(和Date.parse
) 将值解释为UTC时间 :
The
parse
function [...] interprets the resulting String as a date and time;parse
函数将生成的字符串解释为日期和时间。 it returns a Number, the UTC time value corresponding to the date and time.它返回一个数字,即与日期和时间相对应的UTC时间值。
I have already tried modifying the format function to extract the UTC string from the date and see if it displays correctly then, but to no avail.
我已经尝试过修改格式函数以从日期中提取UTC字符串,然后查看它是否正确显示,但无济于事。
date.toUTCString() // => Correctly yields "Tue, 13 May 2008 00:00:00 GMT" moment(date.toUTCString()).format('MM/DD/YY') // => still 05/12/08`
The format that date.toUTCString()
yields is not in any of the formats that moment.js supports , so it falls back to using new Date()
(which interprets the string as UTC time, not local time). date.toUTCString()
产生的格式不是moment.js支持的任何格式 ,因此它回退到使用new Date()
(它将字符串解释为UTC时间,而不是本地时间)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.