最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using Moment.js like PHP's date and strtotime - Stack Overflow

programmeradmin2浏览0评论

I'm a typically server side developer feeling a bit like a fish out of water trying to display time values on the front end. How can I get behavior like PHP's date() and strtotime() functions out of moment.js? I just want a unix timestamp to appear in H:i:s format, and vice versa.

So far I've tried the following, from existing example code and the documentation:

moment(timestamp).format(H:i:s);
moment().duration(timestamp).format(H:i:s);
moment.unix(timestamp).format(h:mm:ss);
moment(formatted,'H:i:s');

Not a SINGLE one of which has worked properly. This may get flagged as duplicate since there are plenty of moment.js questions out there, but I don't know whether it's been updates to the library itself or slightly different context, I have not found one existing solution that has worked for me.

Anybody have any suggestions for these two simple tasks?

EDIT: I've distilled two different problems out of this. One is that functions the moment docs say should work are giving weird values:

moment(1437462000).format('h:mm:ss')

for instance, which should return 7:00:00 utc, returns 10:17:42. This can be fixed in this case by using moment.unix(1437462000).utc().format('h:mm:ss') instead, but this leads into the second problem - the .utc() function seems to get ignored when converting back from a date into a timestamp:

timestamp = moment(formatted,'DD/MM/YYYY H:m:s').utc().unix();

will still return a timezone corrected value (in my case this is incorrect by several hours since the formatted time in question has nothing to do with the client puter) regardless of whether the .utc() function is included or not.

I'm a typically server side developer feeling a bit like a fish out of water trying to display time values on the front end. How can I get behavior like PHP's date() and strtotime() functions out of moment.js? I just want a unix timestamp to appear in H:i:s format, and vice versa.

So far I've tried the following, from existing example code and the documentation:

moment(timestamp).format(H:i:s);
moment().duration(timestamp).format(H:i:s);
moment.unix(timestamp).format(h:mm:ss);
moment(formatted,'H:i:s');

Not a SINGLE one of which has worked properly. This may get flagged as duplicate since there are plenty of moment.js questions out there, but I don't know whether it's been updates to the library itself or slightly different context, I have not found one existing solution that has worked for me.

Anybody have any suggestions for these two simple tasks?

EDIT: I've distilled two different problems out of this. One is that functions the moment docs say should work are giving weird values:

moment(1437462000).format('h:mm:ss')

for instance, which should return 7:00:00 utc, returns 10:17:42. This can be fixed in this case by using moment.unix(1437462000).utc().format('h:mm:ss') instead, but this leads into the second problem - the .utc() function seems to get ignored when converting back from a date into a timestamp:

timestamp = moment(formatted,'DD/MM/YYYY H:m:s').utc().unix();

will still return a timezone corrected value (in my case this is incorrect by several hours since the formatted time in question has nothing to do with the client puter) regardless of whether the .utc() function is included or not.

Share Improve this question edited Aug 12, 2015 at 12:18 Luciasar asked Aug 11, 2015 at 18:07 LuciasarLuciasar 3933 gold badges7 silver badges22 bronze badges 4
  • Are you placing the result of your function on the screen? Or are you generating a value to be used for another function? – Jay Blanchard Commented Aug 11, 2015 at 18:09
  • In the case of trying to convert from a timestamp to a formatted time, it's for display. For the case of going the other way (formatted to timestamp) it's to parse user input to be used by functions. – Luciasar Commented Aug 11, 2015 at 18:11
  • The documentation states that it should be either ´moment(timestamp)` or moment.unix(timestamp). How wrong are these values? How sure are you that your timestamp is correct? – sisve Commented Aug 11, 2015 at 18:23
  • moment(timestamp) returned very strange values for me. moment.unix(timestamp) returned roughly correct stuff, but it was off by 4 hours. Seems like a timezone problem. I know the timestamp is correct because PHP's date() formats the same timestamp just fine on the same page. – Luciasar Commented Aug 11, 2015 at 18:28
Add a ment  | 

2 Answers 2

Reset to default 10

A few things you should realize:

  1. Unix timestamps should always in terms of UTC. They are never adjusted for time zone in numerical form. If they're adjusted for time zone, that's done during the interpretation of the number, not in its representation.

  2. While traditionally a "Unix Timestamp" is in terms of seconds, many environments use milliseconds instead. PHP's date timestamps are based on seconds, while moment and JavaScript's Date object both use milliseconds by default. Using the moment.unix function will let you pass seconds, and is identical to just multiplying the timestamp by 1000.

  3. Moment has two built-in modes, local and UTC. The default mode is local. It doesn't matter what input you provide, if you don't specify UTC, the moment is adjusted to local. To specify UTC, you use the utc function. There are two forms of the function:

    moment.utc(input)   // parsing form
    
    moment(input).utc() // conversion form
    

    Both forms take some input and result in a moment in UTC mode. The difference is in how the input is interpreted. In either case, if the input value is unambiguous, the result is the same. For strings, that means the input would contain either a Z (from ISO8601), or a UTC-based offset. All other forms are ambiguous. For example, if I pass "2015-11-08 01:23:45", I will get different results depending on whether I interpret that string as local time or as UTC.

    For numbers, they are always interpreted as milliseconds in UTC. However, if you use moment(number) without then calling .utc() then the moment is left in local mode, so any output will display as local time.

  4. When you call moment.unix(input), the input is a number of seconds, but the moment is left in local mode. So to display the UTC time, you would use moment.unix(input).utc().

  5. If your pre-recorded timestamps from your other system are in numeric form, but have been adjusted away from UTC, then they are incorrect. You have bad data, and Moment can't help you unless you know specifically how they have deviated and you write code to counteract that.

  6. Moment's formatters are case sensitive. M is months, m is minutes. H is hours on a 24-hour clock, h is hours on a 12-hour clock. Use two consecutive letters when you want to include zero-padding. Example, HH:mm:ss for 13:02:03 vs. h:m:s for 1:2:3.

  7. Moment's X formatter does not care which mode the moment is in. It will always emit seconds in UTC. Likewise, the x formatter returns milliseconds in UTC, as does moment.valueOf().

Also, your last example:

moment.unix(1437462000).utc().format()

Returns "2015-07-21T07:00:00+00:00" - which I believe is the value you expected.

You also get the same original timestamp regardless of which of these you try:

moment.unix(1437462000).utc().format("X") // "1437462000"
moment.unix(1437462000).format("X")       // "1437462000"
moment.unix(1437462000).utc().unix()      // 1437462000
moment.unix(1437462000).unix()            // 1437462000

For anyone who es in and is still looking for direct PHP equivalents for date() and strtotime(), here are the ones I ended up using. Matching up to php basically means just pletely ignoring any kind of local time information by making sure everything is in UTC. That task is a little different between the timestamp->date and date->timestamp cases, though, so you have to be careful.

date()

Converting a timestamp to formatted date without any client timezone correction

var formatted = moment.unix(timestamp).utc().format('h:mm:ss');

strtotime()

Converting a UTC formatted date back to a timestamp without correcting it to local time:

var new_timestamp = moment.utc(formatted_utc,'DD/MM/YYYY H:m:s').format('X')
//where 'DD/MM/YYYY H:m:s' is the formatted date's format, and 
//'X' outputs a unix timestamp without milliseconds. 

Notes:

  • Do not use moment() with parenthesis in the calls: moment().utc(date,format) will return local time values, not your input.
  • Moment.js does not like the use of 'i' for minutes in the formatting, unlike php.
发布评论

评论列表(0)

  1. 暂无评论