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

前端 未结 11 1293
时光说笑
时光说笑 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 10:02

    function setDate(){
        var now = new Date();
        now.setMinutes(now.getMinutes() - now.getTimezoneOffset());
        var timeToSet = now.toISOString().slice(0,16);
    
        /*
            If you have an element called "eventDate" like the following:
    
            
    
            and you would like to  set the current and minimum time then use the following:
        */
    
        var elem = document.getElementById("eventDate");
        elem.value = timeToSet;
        elem.min = timeToSet;
    }
    

提交回复
热议问题