Using Moment.js like PHP's date and strtotime

11,946

Solution 1

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

Solution 2

For anyone who comes 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 completely 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.
Share:
11,946
Luciasar
Author by

Luciasar

Co-op web programmer and data analytics designer at Construction Robotics.

Updated on June 22, 2022

Comments

  • Luciasar
    Luciasar almost 2 years

    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 computer) regardless of whether the .utc() function is included or not.

  • Luciasar
    Luciasar over 8 years
    Thanks for the detailed answer! Unfortunately, it's still missing the one thing I'm still struggling with: getting a formatted UTC time back into a timestamp. I've tried this: new_timestamp = moment(formatted_utc_datetime,'DD/MM/YYYY H:m:s').utc().unix(), but despite the .utc function it still returns a locally corrected timestamp: eg, 7:00 turns into 11:00. Any advice?
  • Luciasar
    Luciasar over 8 years
    Ok I've gotten it working using: moment.utc(formatted,'DD/MM/YYYY H:m:s').format('X'). Your answer is the most complete though so I'll accept it.
  • Matt Johnson-Pint
    Matt Johnson-Pint over 8 years
    Exactly. You want the input string to be interpreted as UTC, so you use the parsing form, rather than the conversion form.
  • Matt Johnson-Pint
    Matt Johnson-Pint over 8 years
    One point you should realize is that .format('X') returns a string, while .unix() returns the same value as a number. Use whichever is most convenient for your scenario.
  • Luciasar
    Luciasar over 8 years
    If I could get your help just one more time, can you explain why moment().utc(formatted, 'format').format('X') would not work? Does moment() with parentheses call the current time and override the utc() function?
  • Matt Johnson-Pint
    Matt Johnson-Pint over 8 years
    moment() gets a moment with the current time. Calling .utc() on an existing moment object is a conversion method. It does not take any parameters, so if you pass any they are ignored. It's only the moment.utc(...) (parsing form) that takes parameters.
  • Matt Johnson-Pint
    Matt Johnson-Pint over 8 years
    Think of moment() as an object initialization function (because of the parenthesis), while the word 'moment' in moment.foo... is like a namespace for containing various static functions.
  • Luciasar
    Luciasar over 8 years
    Gotcha. Thanks again!