Moment js utc() time in london - BST

14,849

Solution 1

Can you use momentJS timezone?

moment().tz('Europe/London');

EDIT: In case you try to use this without seeing the link, it's a separate library you have to include.

Solution 2

If you want the local time instead of the UTC time, just use moment() instead of moment.utc(). You're specifically asking for UTC, so you shouldn't be surprised when you get UTC :)

From the documentation:

By default, moment parses and displays in local time.

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment().

This brings us to an interesting feature of Moment.js. UTC mode.

While in UTC mode, all display methods will display in UTC time instead of local time.

This is assuming you always want the user's local time. If you want a specific time zone (London) which may not be the user's time zone and isn't UTC, then you should use the library indicated by Takuya's answer. I would think carefully before doing so though - while it may be a sensible approach, you should at least validate that first. It's often reasonable to display a time for user U1 in the time zone of user U2 - but here you're using a fixed time zone. That's only appropriate if you know that U2 will always be in London. It would be really confusing if actually U2 is in some other zone - either the same as or different to U1.

Share:
14,849
Arek S
Author by

Arek S

Lead backend engineer Full-Stack software developer Auto-testing, CI, CD advocate Blockchain technologies enthusiast

Updated on June 28, 2022

Comments

  • Arek S
    Arek S almost 2 years

    I'm using momentjs lib to updated text on some ajax action. What I need to do is to set a current date & time in london. I'm using moment.utc() function but because of the summer time I'm one hour out.

    For example running this on 14:26

    console.log( moment.utc().format('HH:mm:ss') );
    

    I'm getting 13:26:53.

    Any idea on how to fix this?