简体   繁体   English

当我使用矩进行解析时,Moment.js 2个不同的日期字符串给出相同的值

[英]Moment.js 2 different date strings when i parse using moment gives same value

I am parsing 2 different date strings 我正在解析2个不同的日期字符串

var d1  = '2014-02-01T00:00:00.000+0530'

var d2 = '2014-02-23T00:00:00.000+0530'   

when i parse them using moment 当我用片刻解析它们时

alert(moment(d1, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());
alert(moment(d2, 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"').toDate());

both of them print Sat Feb 1 2014 xxxxx 他们两个都打印Sat Feb 1 2014 xxxxx

what is wrong with it?? 有什么问题吗?

here is the link to the fiddle i created 这是我创建的小提琴的链接

jsfiddle 的jsfiddle

I think your moment formatting string is causing you the problem. 我认为您瞬间的格式化字符串导致了您的问题。 If I remove this, the dates do not print as the same. 如果我删除了此日期,则日期不会以相同的方式打印。

http://jsfiddle.net/K5ub8/7/ http://jsfiddle.net/K5ub8/7/

EDIT: The specific issue is you are using dd for day, instead of DD . 编辑:特定的问题是您正在使用dd而不是DD http://momentjs.com/docs/#/parsing/string-format/ http://momentjs.com/docs/#/parsing/string-format/

Here is your fiddle fixed: http://jsfiddle.net/K5ub8/9/ 这是您固定的小提琴: http : //jsfiddle.net/K5ub8/9/

However, I am not 100% sure about the fractional seconds, I believe it is SSS instead of fffffff but I would test this if you need to cater for fractional seconds. 但是,我不确定小数秒是100%,我相信它是SSS而不是fffffff但是如果您需要小数秒,我会进行测试。

I should mention that if you are converting it back into a JavaScript date object anyway with toDate() , then you don't really need the moment formatting parameter as the date will be formatted in JSON Date format. 我应该提到的是,如果无论如何都要使用toDate()将其转换回JavaScript日期对象,那么您实际上就不需要瞬间格式化参数,因为日期将以JSON Date格式进行格式化。

I would question why you would want to generate a moment formatted date, and then convert it back to JavaScript, a normal practice might be to receive a date in JavaScript format, then create a moment object which you can use to perform calculations and display in a nice user friendly way. 我会问为什么您要生成一个矩型的日期,然后将其转换回JavaScript,通常的做法是接收JavaScript格式的日期,然后创建一个矩型对象,可用于执行计算并在其中显示用户友好的好方法。

Simple answer: your format was off a bit. 简单的答案:您的格式有点偏离。

http://jsfiddle.net/K5ub8/8/ http://jsfiddle.net/K5ub8/8/

After tweaking the format to be 'YYYY-MM-DDTHH:mm:ss.SSSZZ' rather than 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' it worked just fine. 将格式调整为'YYYY-MM-DDTHH:mm:ss.SSSZZ'而不是'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"'它就可以正常工作了。 When you're trying to debug issues like this, it's always good to keep the format in a separate variable so you can use the same format that you're trying to parse out to display what you're getting. 当您尝试调试此类问题时,最好将格式保留在单独的变量中,以便可以使用尝试解析的格式来显示所得到的内容。 Had you done that, you would have noticed that 'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"' was messed up due to it printing out 2014-01-Fr"T"11:32:03.fffffff"-08:00" . 如果这样做,您会注意到'YYYY-MM-dd"T"HH:mm:ss.fffffff"Z"'由于打印了2014-01-Fr"T"11:32:03.fffffff"-08:00"而被弄乱了2014-01-Fr"T"11:32:03.fffffff"-08:00" Which obviously isn't quite right. 这显然是不正确的。

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

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