Convert date to another timezone in JavaScript

后端 未结 24 3019
没有蜡笔的小新
没有蜡笔的小新 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:18

    Having looked around a lot including links from this page i found this great article, using moment timezone:

    https://www.webniraj.com/2016/11/23/javascript-using-moment-js-to-display-dates-times-in-users-timezone/

    To summarise it:

    Get the user's timezone

    var tz = moment.tz.guess();
    console.info('Timezone: ' + tz);
    

    Returns eg: Timezone: Europe/London

    Set the default user timezone

    moment.tz.setDefault(tz);
    

    Set custom timezone

    moment.tz.setDefault('America/Los_Angeles');
    

    Convert date / time to local timezone, assumes original date/time is in UTC

    moment.utc('2016-12-25 07:00').tz(tz).format('ffffd, Do MMMM YYYY, h:mma');
    

    Returns: Sun, 25th December 2016, 7:00am

    Convert date/time to LA Time

    moment.utc('2016-12-25 07:00').tz('America/Los_Angeles').format('ffffd, Do MMMM YYYY, h:mma');
    

    Returns: Sat, 24th December 2016, 11:00pm

    Convert from LA time to London

    moment.tz('2016-12-25 07:00', 'America/Los_Angeles').tz('Europe/London').format( 'ffffd, Do MMMM YYYY, h:mma' );
    

    Returns: Sun, 25th December 2016, 3:00pm

    0 讨论(0)
  • 2020-11-21 05:19

    quick and dirty manual hour changer and return:

    return new Date(new Date().setHours(new Date().getHours()+3)).getHours()
    
    0 讨论(0)
  • 2020-11-21 05:20

    If you don't want to import some big library you could just use Intl.DateTimeFormat to convert Date objects to different timezones.

    // Specifying timeZone is what causes the conversion, the rest is just formatting
    const options = {
      year: '2-digit', month: '2-digit', day: '2-digit',
      hour: '2-digit', minute: '2-digit', second: '2-digit',
      timeZone: 'Asia/Jakarta',
      timeZoneName: 'short'
    }
    const formatter = new Intl.DateTimeFormat('sv-SE', options)
    const startingDate = new Date("2012/04/10 10:10:30 +0000")
    
    const dateInNewTimezone = formatter.format(startingDate) 
    console.log(dateInNewTimezone) // 12-04-10 17:10:30 GMT+7

    Offsets, daylight saving, and changes in the past will be taken care of for you.

    0 讨论(0)
  • 2020-11-21 05:20

    Time Zone Offset for your current timezone

    date +%s -d '1 Jan 1970'
    

    For my GMT+10 timezone (Australia) it returned -36000

    0 讨论(0)
  • 2020-11-21 05:21

    For moment.js users, you can now use moment-timezone. Using it, your function would look something like this:

    function toTimeZone(time, zone) {
        var format = 'YYYY/MM/DD HH:mm:ss ZZ';
        return moment(time, format).tz(zone).format(format);
    }
    
    0 讨论(0)
  • 2020-11-21 05:22

    var aestTime = new Date().toLocaleString("en-US", {timeZone: "Australia/Brisbane"});
    console.log('AEST time: '+ (new Date(aestTime)).toISOString())
    
    var asiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Shanghai"});
    console.log('Asia time: '+ (new Date(asiaTime)).toISOString())
    
    var usaTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});
    console.log('USA time: '+ (new Date(usaTime)).toISOString())
    
    var indiaTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});
    console.log('India time: '+ (new Date(indiaTime)).toISOString())

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

    0 讨论(0)
提交回复
热议问题