admin管理员组

文章数量:1387463

I'm trying to convert a simple string to a unix timestamp using moment.js

moment('2014-01-14 07:25 PM').unix();
moment('2014-01-14 07:25 AM').unix();

The problem is I get the same result with AM or PM in that string.

1389684300

What gives?

I'm trying to convert a simple string to a unix timestamp using moment.js

moment('2014-01-14 07:25 PM').unix();
moment('2014-01-14 07:25 AM').unix();

The problem is I get the same result with AM or PM in that string.

1389684300

What gives?

Share Improve this question asked Jan 14, 2014 at 0:29 ShealanShealan 1,3463 gold badges20 silver badges34 bronze badges 1
  • I had the same question but needed to convert to ISO string. The solution by @Pekka worked great for me. – Ryan Conaway Commented Oct 4, 2014 at 16:39
Add a ment  | 

2 Answers 2

Reset to default 6

The docs make no mention that your specified format is guaranteed to be correctly recognized. It says

Warning: Browser support for this is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.`

You should probably explicitly specify a format in the second argument.

This should work (JSFiddle):

moment('2014-01-14 07:25 PM', 'YYYY-MM-DD hh:mm A').unix();

It seems to have been a bug in version 2.0.0. Updating to 2.5.0 and the problem is fixed.

本文标签: javascriptConverting simple datetime string in momentjs doesn39t detect AMPMStack Overflow