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

javascript - Convert local time to CET using momentJs - Stack Overflow

programmeradmin3浏览0评论

To convert local time to UTC is easy:

var localTime = "2018-02-27 11:00";    
moment(localTime).utc().format(); // result: "2018-02-27T09:00:00Z"

However I'm not sure if there is a straightforward way to convert my local time to CET (or other timezone such as EST, WEST)

I know that I can do something like this

moment(localTime).tz("Europe/Berlin").format();

which seems to return what I want but still unsure if thats the correct way or not?

To convert local time to UTC is easy:

var localTime = "2018-02-27 11:00";    
moment(localTime).utc().format(); // result: "2018-02-27T09:00:00Z"

However I'm not sure if there is a straightforward way to convert my local time to CET (or other timezone such as EST, WEST)

I know that I can do something like this

moment(localTime).tz("Europe/Berlin").format();

which seems to return what I want but still unsure if thats the correct way or not?

Share Improve this question asked Feb 27, 2018 at 9:55 philomathphilomath 2,2096 gold badges34 silver badges46 bronze badges 3
  • Yes, it is the the correct way to convert a moment object to Europe/Berlin timezone using moment-timezone. – VincenzoC Commented Feb 27, 2018 at 10:03
  • ok so what if I want to convert to EST or WEST? – philomath Commented Feb 27, 2018 at 10:49
  • 1 Then you pass whichever time zone identifier you want. (Note that abbreviations are not generally valid identifiers). – Matt Johnson-Pint Commented Feb 27, 2018 at 18:17
Add a ment  | 

1 Answer 1

Reset to default 4

You can use the moment-timezone

since it accounts for daylight saving.

moment(momentTZ().tz("Europe/Berlin")).format();

If you do not mind for daylight saving moment itself is fine.

发布评论

评论列表(0)

  1. 暂无评论