How to Convert a Date String to UTC date using Javascript?

This question already has an answer here:

  • How do you convert a JavaScript date to UTC? 22 answers

  • try toISOString() on the Date object. If using less than ECMAScript 5, there is a polyfill for the function

    if ( !Date.prototype.toISOString ) {
      ( function() {
    
        function pad(number) {
          if ( number < 10 ) {
            return '0' + number;
          }
          return number;
        }
    
        Date.prototype.toISOString = function() {
          return this.getUTCFullYear() +
            '-' + pad( this.getUTCMonth() + 1 ) +
            '-' + pad( this.getUTCDate() ) +
            'T' + pad( this.getUTCHours() ) +
            ':' + pad( this.getUTCMinutes() ) +
            ':' + pad( this.getUTCSeconds() ) +
            '.' + (this.getUTCMilliseconds() / 1000).toFixed(3).slice( 2, 5 ) +
            'Z';
        };
    
      }() );
    }
    

    There is an UTC method of Date. It should be as simple as:

    new Date(Date.UTC(a[2],parseInt(a[1], 10) - 1,a[0]));
    
    链接地址: http://www.djcxy.com/p/46590.html

    上一篇: 日期选择器在VueJS元素库中返回具有时区的dateTime

    下一篇: 如何使用Javascript将日期字符串转换为UTC日期?