user7329277
user7329277

Reputation: 17

Convert date to ISOString javascript server side?

i have a date with this format" 20/02/2018 14:40:00 CET" how can i convert it with the format ISOString, i tried this code but i haven't any result !!

function myFunction() {
    var d = '20/02/2018 14:40:00 CET';
    var n = d.toISOString();
    document.getElementById("demo").innerHTML = n;
}

Upvotes: -2

Views: 205

Answers (2)

mplungjan
mplungjan

Reputation: 177796

You need to split and re-assemble since European date format (20/02) is not parse-able

var d = '20/02/2018 14:40:00 CET',
  parts = d.split(" "),
  time = parts[1].split(":"),
  dParts = parts[0].split("/");

+time[0]--; // CET is +1 - for more https://en.wikipedia.org/wiki/List_of_time_zone_abbreviations

var n = dParts[2] + "-" +
        dParts[1] + "-" +
        dParts[0] + "T" +
        time[0] + ":" +
        time[1] + ":" +
        time[2] + ".000Z";
console.log(n);

Upvotes: 0

Léo R.
Léo R.

Reputation: 2698

You need to create a new date instance before try use toISOString method, however correct date format is mm/dd/yyyy so try this :

var d = new Date("02/20/2018 14:40:00");
var n = d.toISOString();
console.log(n);

Upvotes: 0

Related Questions