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

前端 未结 11 1298
时光说笑
时光说笑 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:40

    No moment.js needed: Here's a full round trip answer, from an input type of "datetime-local" which outputs an ISOLocal string to UTCseconds at GMT and back:

    
    
    isoLocal="2020-02-16T19:30"
    utcSeconds=new Date(isoLocal).getTime()/1000
    
    //here you have 1581899400 for utcSeconds
    
    let isoLocal=new Date(utcSeconds*1000-new Date().getTimezoneOffset()*60000).toISOString().substring(0,16)
    2020-02-16T19:30
    

提交回复
热议问题