Using timezones with moment.js fromNow() or from()

Solution 1

Ideally, you would want to pass a UTC timestamp from your server to the client. That doesn't mean you have to switch your whole server over to UTC, it just means that you would convert from the time in your database to UTC on the server before sending it over the web. Sure, it would be even better if you actually stored times in UTC, but you said you aren't in a position to make that sort of change right now. But let's just work off the assumption that you can't change anything at all on the server.

We'll also assume that your server is fixed to the UTC-07:00 offset. In real life, this would only be true for places like Arizona that don't follow daylight saving time. So if you are in Los Angeles and are in Pacific Time, then some of your data is based on UTC-07:00, but some of it is based on UTC-08:00. That requires a lot more work if you want to do it in JavaScript.

Let's also assume that the input is already a string in ISO8601 format. (If it's not, then let me know and I will adjust this code.)

var s = "2013-09-11 18:00:00";  // from action.timeStamp

var actionTime = moment(s + "-07:00", "YYYY-MM-DD HH:mm:ssZ");

var timeAgo = actionTime.fromNow();

The reason your other code didn't work is because in the first line, you are affected by the time zone of the browser. The zone setter in the second line just changes the zone for formatting, not changing the actual moment in time.

Also, when you dump a moment to the console for debugging, make sure you format it for output. Otherwise you are just looking at its internal property values, which may or may not make sense directly.

Solution 2

I have solved it in a different way, maybe this option was not possible back when the question was asked, but might be easier now.

I used moment-timezone.js (which requires moment.js 2.6.0+).

I set the default timezone to my server's timezone like this:"America/New_York"); // "America/New_York" in my case

and then just use it normally. fromNow() will use the timezone in the client to calculate the time that has passed since that moment.


Solution 3

i had the same issue and used the above comments to modify my code. I did the following to get it resolved:

  transform(value: string) {
    var time = moment(value).utc();
    return moment(time, "YYYYMMDD").fromNow();

I was missing the .utc() to convert it before I applied the .fromNow()

Things to note this is being used within a Pipe for Ionic 3 and the above code is from the pipe logic.

Author by


Updated on June 13, 2022


  • Admin
    Admin about 2 months