javascript date - preserve timezone offset

前端 未结 1 1938
一整个雨季
一整个雨季 2021-01-18 09:44

I have a ISO8601 date that contains a timezone offset (see below). When I create a Date object from this, the date object is converted into my timezone (currently GMT), and

相关标签:
1条回答
  • 2021-01-18 10:15

    Not with the built-in Date object, as they're only aware of Local (as defined by the user's browser and/or OS settings) and UTC. You can see this from the many cloned methods the class has (e.g., getHours / getUTCHours).

    getTimezoneOffset is the only timezone info you really have, but it's local as well and will probably only give you +0 again (or +6 in my case):

    var date = new Date("2012-01-17T12:55:00.000+01:00");
    console.log(date.getTimezoneOffset() / 60.0);
    

    You can try timezone-js (or one of its forks), but you'll need to know the Olson timezone name not just the GMT/UTC offset:

    var date = new new timezoneJS.Date('2012-01-17T12:55:00.000+01:00', 'Europe/Brussels');
    alert(date.getTimezoneOffset() / 60.0); // +1
    
    0 讨论(0)
提交回复
热议问题