Search code examples
javascriptdatedatetimehourminute

How to get the total number of minutes from a date string?


From the back-end, I am retrieving this string as a date: "2017-08-16T18:35:41.611Z2017-08-16T13:35:41.611"

How can I extract the total number of minutes from this string? I tried the following:

let date = new Date("2017-08-16T18:35:41.611Z2017-08-16T13:35:41.611");
let minutes = date.getMinutes();
console.log(minutes);

But date logs as Invalid Date and minutes logs as NaN. Any ideas?


Solution

  • Your backend is sending you twice the date 2017-08-16T18:35:41.611Z2017-08-16T13:35:41.611

    If you can't modify the backend service, you could split them by the Z and then do the function you said:

    let date = new Date("2017-08-16T18:35:41.611Z2017-08-16T13:35:41.611".split("Z")[0]);
    let minutes = date.getMinutes();
    console.log(minutes);