How do you convert a JavaScript date to UTC?

后端 未结 29 2049
无人共我
无人共我 2020-11-22 00:50

Suppose a user of your website enters a date range.

2009-1-1 to 2009-1-3

You need to send this date to a server for some processing, but th

29条回答
  •  名媛妹妹
    2020-11-22 01:07

    Date.prototype.toUTCArray= function(){
        var D= this;
        return [D.getUTCFullYear(), D.getUTCMonth(), D.getUTCDate(), D.getUTCHours(),
        D.getUTCMinutes(), D.getUTCSeconds()];
    }
    
    Date.prototype.toISO= function(){
        var tem, A= this.toUTCArray(), i= 0;
        A[1]+= 1;
        while(i++<7){
            tem= A[i];
            if(tem<10) A[i]= '0'+tem;
        }
        return A.splice(0, 3).join('-')+'T'+A.join(':');    
    }
    

提交回复
热议问题