I'm trying to subtract two times from each other and for the most part works but in some scenarios it's giving the wrong output.
Here's my code so far:
//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)
So like I said, it works but if the time difference is more than one hour, it's starts giving weird outputs. For example if the real time difference has been only 38 minutes and the current time is an hour ahead, it will count it as 1 hours and 22 minutes.
Any suggestions how I can do this better?