【问题标题】:Subtracting date time of UTC format in Javascript [duplicate]在Javascript中减去UTC格式的日期时间[重复]
【发布时间】:2015-04-23 23:23:58
【问题描述】:

我试图从彼此中减去两次,并且在大多数情况下有效,但在某些情况下它给出了错误的输出。

到目前为止,这是我的代码:

  //postDateTime looks like 2015-04-23T22:23:32.902Z

  //Split the date and time
  var postDateTime2 = postDateTime.split("T");

  //remove the Z from the post time stamp
  postDateTime2[1] = postDateTime2[1].replace('Z','');


  //split the post date/time into sperate variables
  var postDate = postDateTime2[0]; // date
  var postTime = postDateTime2[1]; // time

  //split up the post time into hours, minutes, seconds
  var postTimeSplit = postTime.split(":");
  var postTimeHour = postTimeSplit[0];
  var postTimeMinutes = postTimeSplit[1];
  var postTimeSeconds = postTimeSplit[2];

  //split the date to have year, month, date separate
  var postDateSplit = postDate.split("-");

  //split the post date to year, month, date
  var postYear = postDateSplit[0];  //year
  var postMonth = postDateSplit[1]; //month
  var postDate2 = postDateSplit[2]; //date

  //get the current hour, minutes, seconds in UTC time.
  var hours = now.getUTCHours();
  var minutes2 = now.getUTCMinutes();
  var seconds2 = now.getUTCSeconds();



  //get the difference in years between post time and response time
  var responseYear = Math.abs(now.getUTCFullYear() - postYear); 
  //get the difference in months between post time and response time
  var responseMonth = Math.abs(mm - postMonth); 
  //get the difference in days between post time and response time
  var responseDate = Math.abs(now.getUTCDate() - postDate2); 
  //get the difference in hours between post time and response time
  var responseHour = Math.abs(now.getUTCHours() - postTimeHour);
  //get the difference in minutes between post time and response time 
  var responseMinutes = Math.abs(now.getUTCMinutes() - postTimeMinutes);
  //get the difference in seconds between post time and response time
  var responseSeconds = Math.abs(now.getUTCSeconds() - postTimeSeconds); 

  Math.round(responseSeconds); // round the seconds to up to 2 decimal (doesn't work as expected)

就像我说的,它可以工作,但如果时差超过一小时,它就会开始给出奇怪的输出。例如,如果实际时差只有 38 分钟,而当前时间提前一个小时,则将其计为 1 小时 22 分钟。

有什么建议可以让我做得更好吗?

【问题讨论】:

  • 为了更好地解释问题,看这个例子:2015-04-23 23:33:17 - 2015-04-23 23:23:49 = 它给出的输出是 10 分钟和 32.005 秒,但实际上是 0:09:28
  • 不要拆分字符串,而是使用Date 函数来获取您需要的部分。可能更容易的是使用.getTime() 获取毫秒刻度并对两个整数进行简单的差分,然后从毫秒确定整个时间单位。
  • Jasen,我可以为当前日期/时间执行此操作,但我想从中减去它的日期/时间被格式化为 RFC 3339 时间戳,我无法更改它。也许我可以在那个 RFC 3339 时间戳中转换我的当前时间,然后减去它们?
  • 您可以将该字符串解析为日期:new date("2015-04-23T22:23:32.902Z")

标签: javascript


【解决方案1】:

因此,经过更多研究并感谢 Jasen,将我的当前时间转换为 RFC 3339 时间戳,然后以毫秒为单位找到差异并将其转换回日期更容易。为了便于阅读,我还做了一些额外的格式化。这是我的最终代码:

//current time in RFC 3339 timestamp
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
    + pad(d.getUTCMonth()+1)+'-'
    + pad(d.getUTCDate())+'T'
    + pad(d.getUTCHours())+':'
    + pad(d.getUTCMinutes())+':'
    + pad(d.getUTCSeconds())+'Z'
}

var d = new Date();
var currentTime = ISODateString(d);

//assign vars for current hour, minutes, seconds in UTC time.
var hours = d.getUTCHours();
var minutes = d.getUTCMinutes();
var seconds = d.getUTCSeconds();

//parse both time stamps into dates
var finalCurrentTime = Date.parse(currentTime);
var finalPostDateTime = Date.parse(postDateTime);

//find the difference between the original post date/time and the current date/time (in milliseconds)
var responseTimeFinal = Math.abs(finalCurrentTime - finalPostDateTime);


function dhm(ms){
var days2 = Math.floor(ms / (24*60*60*1000));
var daysms=ms % (24*60*60*1000);
var hours2 = Math.floor((daysms)/(60*60*1000));
var hoursms=ms % (60*60*1000);
var minutes2 = Math.floor((hoursms)/(60*1000));
var minutesms=ms % (60*1000);
var sec = Math.floor((minutesms)/(1000));

days2 = (days2 < 10) ? "0" + days2 : days2;
hours2 = (hours2 < 10) ? "0" + hours2 : hours2;
minutes2 = (minutes2 < 10) ? "0" + minutes2 : minutes2;
sec = (sec < 10) ? "0" + sec : sec;

  //format day
  if (days2 === "00"){
    days2 = "";
  } else if (days2 === "01"){
    days2 = days2 + " day, ";
  } 
  else if (days2 > "01"){
    days2 = days2 + " days, ";
  }

  //format hours
  if (hours2 === "00"){
    hours2 = "";
  }   
  else if (hours2 === "01"){
    hours2 = hours2 + " hour, ";
  } 
  else if (hours2 > "01"){
    hours2 = hours2 + " hours, ";
  }

  //format minutes
  if (minutes2 === "01"){
    minutes2 = minutes2 + " minute, ";
  } 
  else if (minutes2 > "01"){
    minutes2 = minutes2 + " minutes, ";
  }

  //format seconds
  if (sec === "01"){
    sec = sec + " second";
  } 
  else if (sec > "01"){
    sec = sec + " seconds";
  }

return days2+""+hours2+""+minutes2+""+sec;
}

//pass the milliseconds from responseTimeFinal into the dhm function to convert it back to a date
var timeDifference = dhm(responseTimeFinal);

console.log(timeDifference);  // our final result that works!

【讨论】:

    猜你喜欢
    • 2014-08-08
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 2020-09-28
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多