简体   繁体   English

JavaScript-使用当前时区格式化日期

[英]JavaScript - formatting date with current timezone

I am using the moment.js library. 我正在使用moment.js库。 I have dates in this format: "2014-06-19T18:00:00+02:00" (string). 我的日期格式如下: "2014-06-19T18:00:00+02:00" (字符串)。 I want to display the date formatted with the user's timezone. 我想显示用用户时区格式化的日期。

moment.js docs specifies to use moment("2014-06-19T18:00:00+02:00").utc().format() moment.js文档指定使用moment("2014-06-19T18:00:00+02:00").utc().format()

My questions are: 我的问题是:

  • How can I set the timezone once and not have to put .utc() every time? 如何设置一次时区,而不必每次都放置.utc()

  • How can I test the timezone functionality? 如何测试时区功能? Should I change my computer's clock? 我应该改变电脑的时钟吗?

Calling .utc() would actually translate the value you put in to UTC. 调用.utc()实际上会将您输入的值转换为UTC。 If you want the user's local time zone, then you should just omit that and call .format() . 如果您想要用户的本地时区,则应忽略该设置并调用.format()

That you passed a +02:00 offset with the original value doesn't matter. 您通过原始值传递了+02:00偏移量没关系。 By default, moment will adjust the value to the user's time zone. 默认情况下,moment会将值调整为用户的时区。

On the other hand, if you want to keep the offset you provided, regardless of the user's time zone, then you can use the parseZone function. 另一方面,如果您想保留您提供的偏移量,而不考虑用户的时区,则可以使用parseZone函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM