Moment js utc () time in London - BST

I am using momentjs lib to update text for some ajax action. I need to do to set the current date and time in London. I am using moment.utc () function but due to daylight saving time I am one hour.

For example, by running this at 14:26

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

      

I get 13:26:53.

Any idea on how to fix this?

+3


source to share


2 answers


Can you use timeJS timezone ?

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



EDIT: If you try to use this without seeing the link, it is a separate library that you should include.

+7


source


If you want to use local time instead of UTC time, use moment()

instead moment.utc()

. You are specifically asking for UTC, so don't be surprised when you get UTC :)

From the documentation:



By default, the moment is analyzed and displayed in local time.

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

instead moment()

.

This brings us to the interesting feature of Moment.js. UTC.

In UTC mode, all display methods will be displayed in UTC instead of local time.

It is assumed that you always want the user's local time. If you want a specific timezone (London) that cannot be the user's timezone and is not UTC, then you should use the library mentioned in Takui's answer. I would think carefully before doing this, although while this may be a sensible approach, you should at least approve it first. It is often wise to display the time for user U 1 in the timezone of user U 2but here you are using a fixed timezone. This is only suitable if you know that U 2will always be in London. It would be really confusing if actually U 2 is in some other zone - either the same as or different from U 1...

+2


source







All Articles