Convert date to another timezone in JavaScript

后端 未结 24 3042
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 04:45

I am looking for a function to convert date in one timezone to another.

It need two parameters,

  • date (in format \"2012/04/10 10:10:30 +0000\")
24条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-21 05:23

    People familiar with the java 8 java.time package, or joda-time will probably love the new kid on the block: the js-joda library.

    Install

    npm install js-joda js-joda-timezone --save
    

    Example

    
    
    
    

    In true java nature, it's pretty verbose lol. But, being a ported java library, especially considering they ported 1800'ish test cases, it also probably works superbly accurately.

    Chrono manipulation is hard. That's why many other libraries are buggy in edge cases. Moment.js seems to get timezones right, but the other js libs I've seen, including timezone-js, don't seem trustworthy.

提交回复
热议问题