Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between two times in am/pm format in javascript

Tags:

javascript

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.

like image 743
John Mathew Avatar asked May 12 '26 17:05

John Mathew


1 Answers

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.

like image 106
Lajos Arpad Avatar answered May 15 '26 07:05

Lajos Arpad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!