I have this date field in UTC format say
dt1 = "2020-03-27T16:59:57Z"
and another date field in local format say
dt2 = "2020-03-27 16:00:00"
I need to find the differnece in minutes between dt1 and dt2. Basically I need to remove the T and Z in dt1 datefield and do the difference.
Here is what I tried:
var diff = (dt1 - dt2) / 1000;
diff /= 60;
return Math.abs(Math.round(diff));
But it return NaN as output. please provide a fix for this!
I have this date field in UTC format say
dt1 = "2020-03-27T16:59:57Z"
and another date field in local format say
dt2 = "2020-03-27 16:00:00"
I need to find the differnece in minutes between dt1 and dt2. Basically I need to remove the T and Z in dt1 datefield and do the difference.
Here is what I tried:
var diff = (dt1 - dt2) / 1000;
diff /= 60;
return Math.abs(Math.round(diff));
But it return NaN as output. please provide a fix for this!
Share Improve this question asked Mar 27, 2020 at 11:46 Mar1009Mar1009 8112 gold badges13 silver badges29 bronze badges 2- There is no "UTC format". UTC is a time standard, it doesn't define a format. Perhaps you mean ISO 8601 format. – RobG Commented Mar 27, 2020 at 11:50
- @RobG isn't the first format iso? I would e here and check if any if the 'toX' methods works for you. There are quite a few Like toISOString or to toGMTString > developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – The Fool Commented Mar 27, 2020 at 11:55
1 Answer
Reset to default 7You should transform the strings to dates so that you can perform operations with the dates:
var dt1 = new Date("2020-03-27T16:59:57Z".replace('T', ' ').replace('Z', ''))
var dt2 = new Date("2020-03-27 16:00:00")
function calcDifference(dt1, dt2) {
var diff = (dt1 - dt2) / 1000;
diff /= 60;
return Math.abs(Math.round(diff));
}
console.log(calcDifference(dt1, dt2));