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
2 Answers
Reset to default 10You 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
版权声明:本文标题:difference between two times in ampm format in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741304052a2371263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论