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

javascript - Moment js utc() time in london - BST - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question asked Aug 4, 2014 at 13:28 Arek SArek S 4,7194 gold badges26 silver badges35 bronze badges 3
  • Why are you using utc() if you want a local time? Surely it's behaving exactly as expected... – Jon Skeet Commented Aug 4, 2014 at 13:34
  • I don't want local time but London time – Arek S Commented Aug 4, 2014 at 13:51
  • Regardless of where the user is? That's going to give quite an odd experience, I'd say - but Takuya's answer should have you covered. Could you explain more about why you want this? – Jon Skeet Commented Aug 4, 2014 at 13:52
Add a comment  | 

2 Answers 2

Reset to default 13

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.

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.

发布评论

评论列表(0)

  1. 暂无评论