I want find the difference between two time in the format of AP/PM in jquery.
i had tested many codes but can't find any solution.
One of my code is here.
var timeStart = new Date("01/01/2007 " + "05.00 AM").getHours();
var timeStartmin = new Date("01/01/2007 " + "05.00 AM").getMinutes();
var timeEnd = new Date("01/01/2007 " + "10.30 PM").getHours();
var timeEndmin = new Date("01/01/2007 " + "10.30 PM").getMinutes();
timeStart = timeStart+"."+timeStartmin;
timeEnd = timeEnd+"."+timeEndmin;
var hourDiff = timeEnd-timeStart;
alert(hourDiff);
but it doesn't works properly. Can any body give me an advise.
You initialize your dates incorrectly. This is how you should do it:
var timeStart = new Date("01/01/2007 " + "05:00 AM");
var timeEnd = new Date("01/01/2007 " + "10:30 PM");
var diff = (timeEnd - timeStart) / 60000; //dividing by seconds and milliseconds
var minutes = diff % 60;
var hours = (diff - minutes) / 60;
Then you can do with hours and minutes whatever you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With