0

MVC5

I'm looking to compare enddate and startdate for a booking to find the total number of hours in between. Datetime is in format of DD/MM/YYYY HH:MM

var hours = (endDate - startDate).TotalHours;
alert('Total Hours Difference ' + hours);

Output is showing "Total Hours Difference UNDEFINED"

WORKING SOLUTION

startDate = parseInt(new Date(startDate).getTime() / 1000);
endDate = parseInt(new Date(endDate).getTime() / 1000);
var timeDiff = (endDate - startDate) / 3600;  // will give difference in hrs
alert('Total Hours Differece' + timeDiff);
  • How the values look for `endDate` and `startDate` – Prashant Pimpale Jan 23 '19 at 14:00
  • 1
    Although that suspiciously looks like javascript and not c# - especially as it is showing undefined rather than throwing a null value exception - if that's the case, then take a look at https://stackoverflow.com/questions/32776101/difference-in-hours-between-two-dates-and-times-by-javascript – Pete Jan 23 '19 at 14:06
  • Ah sorry yes, i'm still grasping mvc at the moment, It would be javascript. StartDate = 23/01/2019 14:00 EndDate = 23/01/2019 15:00 – Christopher Shiels Jan 23 '19 at 14:09
  • Thank you for your response @Pete this has resolved it – Christopher Shiels Jan 23 '19 at 14:56

0 Answers0