Home > other >  Convert date time string to UTC date in JavaScript
Convert date time string to UTC date in JavaScript

Time:10-01

How can I convert "2011-09-30T00:00:00" date time string to UTC date in JavaScript?

I tried new Date("2011-09-30T00:00:00") but it converts to "2011-09-29T23:00:00.000Z".

CodePudding user response:

function createDateAsUTC(dateYmd) {
    var dateYmdSplited = dateYmd.split('-');
    var y = Number(dateYmdSplited[0]);
    var m = Number(dateYmdSplited[1]) - 1;
    var d = Number(dateYmdSplited[2])

    return new Date(Date.UTC(y, m, d, 0, 0, 0))
}

var fecha = "2021-09-01";
var d = createDateAsUTC(fecha);

CodePudding user response:

Simple:

function createDateUTC(dateUTC) {
    return new Date(dateUTC   "Z");
}

var dateUTC = createDateUTC("2011-09-30T00:00:00");

console.log(dateUTC);

  • Related