How to ISO 8601 format a Date with Timezone Offset in JavaScript?

前端 未结 11 1549
时光说笑
时光说笑 2020-11-22 09:32

Goal: Find the local time and UTC time offset then construct the URL in following format.

Example URL: /Actions/Sleep?dura

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 09:39

    Here are the functions I used for this end:

    function localToGMTStingTime(localTime = null) {
        var date = localTime ? new Date(localTime) : new Date();
        return new Date(date.getTime() + (date.getTimezoneOffset() * 60000)).toISOString();
    };
    
    function GMTToLocalStingTime(GMTTime = null) {
        var date = GMTTime ? new Date(GMTTime) : new Date();;
        return new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString();
    };
    

提交回复
热议问题