Convert any date to UTC timezone date – Javascript

console.log("Any timezone date to UTC");
console.log(AnyTimezoneDateToUTC("2020-05-08 16:19:17"));

function AnyTimezoneDateToUTC(date) {

    /* Date string */
    var dateString = new Date(date).toUTCString();
    console.log(dateString);
    // result will be : Fri, 08 May 2020 10:49:17 GMT

    /* Date object */
    var dateObject = new Date(date);
    var utcYear = dateObject.getUTCFullYear();

    var utcMonth = dateObject.getUTCMonth() + 1;
    if (utcMonth < 10) {
        utcMonth = '0' + utcMonth;
    }

    var utcDate = dateObject.getUTCDate();
    if (utcDate < 10) {
        utcDate = '0' + utcDate;
    }

    var utcHours = dateObject.getUTCHours();
    var ampm = utcHours >= 12 ? 'PM' : 'AM';

    var utcMinutes = dateObject.getUTCMinutes();
    utcMinutes = utcMinutes < 10 ? '0' + utcMinutes : utcMinutes;

    var utcSeconds = dateObject.getUTCSeconds();

    var finalDate = utcYear + "-" + utcMonth + "-" + utcDate + " " + utcHours + ":" + utcMinutes + ":" + utcSeconds + " " + ampm;
    return finalDate;

    // result will be : 2020-05-08 10:49:17 AM
}

 

+1
1
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0