admin管理员组

文章数量:1278921

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.

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.

Share Improve this question edited Aug 1, 2017 at 10:05 Lajos Arpad 77k40 gold badges117 silver badges222 bronze badges asked Dec 19, 2014 at 7:53 John MathewJohn Mathew 4051 gold badge4 silver badges16 bronze badges 1
  • 1 whats in valuestop? – Cerlin Commented Dec 19, 2014 at 7:58
Add a ment  | 

2 Answers 2

Reset to default 10

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.

first of all Date() will not accept "01/01/2007 " + "05.00 PM" it will return NaN on getHours() and on other also.so you need to pass

var timeStart = new Date("01/01/2007 " + "05:00:00").getHours();

and do this:

var diffhour     =   parseInt(timeEnd)-parseInt(timeStart);
var diffmin     =   parseInt(timeStartmin)-parseInt(timeEndmin);

var Diff = diffhour+":"+diffmin;    
alert(Diff);

本文标签: difference between two times in ampm format in javascriptStack Overflow