简体   繁体   English

使用moment.js的“无效日期”解析日期

[英]“Invalid date” parsing date with moment.js

I'm trying to calculate the relative date from a given one like the following 我正在尝试从给定的日期计算相对日期,如下所示

Mon Nov 21 11:48:33 CET 2016 2016年11月21日星期一11:48:33 CET

I think the given date follows this pattern: 我认为给定日期遵循以下模式:

EEE MMM d HH:mm:ss zzz yyyy EEE MMM d HH:mm:ss zzz yyyy

So, I'm trying to get the relative date with this line: 因此,我正在尝试通过此行获取相对日期:

moment("Mon Nov 21 11:48:33 CET 2016", "EEE MMM d HH:mm:ss zzz yyyy").fromNow();   

But I'm receiving an "Invalid date"... 但是我收到一个“无效日期” ...

I've changed the "EEE" for "ddd" following some advices but then I'm getting a bad relative date. 我已按照一些建议将“ ddd”的“ EEE”更改为“ ddd”,但是相对日期却不好。

moment("Mon Nov 21 12:30:40 CET 2016", "ddd MMM d HH:mm:ss zzz yyyy").fromNow() > 20 days ago moment(“ 2016年11月21日星期一12:30:40”,“ ddd MMM d HH:mm:ss zzz yyyy”)。fromNow() > 20天前

Any idea about what I'm doing wrong? 关于我在做什么错的任何想法吗?

Thank you so much! 非常感谢!

Assuming you're using moment-timezone in addition to Moment, there are a few problems: 假设除了Moment之外,您还使用了矩时区 ,那么还有一些问题:

  1. zzz is invalid undocumented; zzz 无效,未记录; it's just z . 只是z (It seems to be allowed, though.) (不过,似乎允许这样做。)
  2. EEE is invalid. EEE无效。 E is for day numbers. E是天数。 You wanted ddd (day name). 您想要ddd (日期名称)。 See the documentation . 请参阅文档
  3. You've also used d (day name) where you wanted D or DD (day of month number). 您还想在DDD (月号)中使用d (日期名称)。 Again, see the docs linked above. 同样,请参阅上面链接的文档。

This works with moment+moment-timezone: 这适用于moment + moment-timezone:

moment("Mon Nov 21 11:48:33 CET 2016", "ddd MMM D HH:mm:ss z yyyy").fromNow();
// -------------------------------------^^^-----^----------^

Example: 例:

 console.log(moment("Mon Nov 21 11:48:33 CET 2016", "ddd MMM D HH:mm:ss z yyyy").fromNow()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.9/moment-timezone-with-data-2010-2020.min.js"></script> 

However , I don't think Moment supports parsing that z , because I can't find anything in the Moment Timezone docs saying it enhances parsing, and when I run the above, it acts as though the date/time is in my timezone (GMT), not CET; 但是 ,我不认为Moment支持解析z ,因为在Moment时区文档中找不到任何内容可以说它增强了解析,并且当我运行上述代码时,它的作用就好像日期/时间在我的时区中( GMT),而不是CET; and in fact I can swap in any timezone indicator I want (EST, PST, etc.) and it still treats the string as my local time. 实际上,我可以交换我想要的任何时区指示符(EST,PST等),并且仍然将字符串视为本地时间。

For Day of the week use ddd 对于星期几,请使用ddd

Check http://momentjs.com/docs/ for more information on formats. 检查http://momentjs.com/docs/以获取有关格式的更多信息。

 console.log(moment("Mon Nov 21 11:48:33 CET 2016", "ddd MMM d HH:mm:ss zzz yyyy").fromNow()); 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.16.0/moment.min.js"></script> 

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

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