在Javascript中减去UTC格式的日期时间

时间:2015-04-23 23:23:58

标签: javascript

我试图相互减去两次,但大多数情况下都有效,但在某些情况下,它输出错误。

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

  //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分钟。

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

1 个答案:

答案 0 :(得分:2)

因此经过一些更多的研究并感谢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!