最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

datetime - remove T and Z in date field in Javascript - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

You 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));

发布评论

评论列表(0)

  1. 暂无评论