我希望在minuts中的2个日期之间有所不同,但我尝试了很多方法,但没有得到真正的区别
我试过
var punchIn = "Fri Apr 21 2017 10:57:11 GMT+0530 (IST)";
var punchOut ="Fri Apr 21 2017 13:19:50 GMT+0530 (IST)";
var startDate = new Date(punchIn);
var endDate = new Date(punchOut);
var workingHours = (endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60);
(endDate.getTime() - startDate.getTime())= 8559000将其转换为 它给出的时间 2.3775
但它应该是2小时22分钟..
答案 0 :(得分:0)
var workingHours = (endDate.getTime() - startDate.getTime()) / (1000 * 60);
这完美地将其转换为分钟,但是当您执行另一个* 60
时,它会将时间分成几小时。但是有一个转折点。那些0.3775是22分钟。只是因为它转换为小时并且它甚至可以达到0.9,但这些是分钟,只是在小数后面转换。因为没有时间元素。
也许您可以在var workingHours = new Date ((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60));
上创建另一个Date对象,就像这样或者其他东西,但这可以让你弄明白。
编辑:
var punchIn = "Fri Apr 21 2017 10:57:11 GMT+0530 (IST)";
var punchOut ="Fri Apr 21 2017 13:19:50 GMT+0530 (IST)";
var startDate = new Date(punchIn);
var endDate = new Date(punchOut);
var millis = (endDate.getTime() - startDate.getTime());
var second = (millis / 1000) % 60;
var minute = Math.floor(millis / (1000 * 60)) % 60;
var hour = Math.floor(millis / (1000 * 60 * 60)) % 24;
var time = hour + ":" + minute + ":" + second;
document.getElementById("time").innerHTML += time;

<div id="time"></div>
&#13;
答案 1 :(得分:0)
检查出来:
//<![CDATA[
// external.js
var doc, bod, htm, C, E, T, TimeSpan; // for use on other loads
addEventListener('load', function(){
// Maybe you'll learn something here
doc = document; bod = doc.body; htm = doc.documentElement;
C = function(tag){
return doc.createElement(tag);
}
E = function(id){
return doc.getElementById(id);
}
T = function(tag){
return doc.getElementsByTagName(tag);
}
TimeSpan = function(startDate, endDate){
var workingHours = (endDate.getTime()-startDate.getTime())/3600000;
this.hours = Math.floor(workingHours);
var ml = (workingHours-this.hours).toString().length-2, workingMinutes = (workingHours*Math.pow(10, ml)-this.hours*Math.pow(10, ml))*60/Math.pow(10, ml);
this.minutes = Math.floor(workingMinutes);
var sl = (workingMinutes-this.minutes).toString().length-2;
this.seconds = Math.round((workingMinutes*Math.pow(10, sl)-this.minutes*Math.pow(10, sl))*60/Math.pow(10, sl));
}
var form = E('form');
form.addEventListener('sumbit', function(ev){
ev.preventDefault();
});
var start = E('start'), stop = E('stop'), btn = E('btn');
start.value = 'Fri Apr 21 2017 10:57:11 GMT+0530 (IST)';
stop.value = 'Fri Apr 21 2017 13:19:50 GMT+0530 (IST)';
function displaySpan(){
var span = new TimeSpan(new Date(start.value), new Date(stop.value));
E('output').innerHTML = 'Hours: '+span.hours+'<br />Minutes: '+span.minutes+'<br />Seconds: '+span.seconds;
}
btn.addEventListener('click', displaySpan);
var els = form.elements;
for(var i=0,fe,l=els.length; i<l; i++){
fe = els[i];
fe.addEventListener('keypress', function(ev){
if(ev.keyCode === 13)displaySpan();
});
}
});
//]]>
&#13;
/* external.css */
html,body{
padding:0; margin:0;
}
.main{
width:980px; margin:0 auto;
}
#form{
width:600px; height:50px; background:green; color:#fff; padding:10px;
}
#form>label{
width:40px;
}
#form>label,#form>input[type=text]{
display:block; float:left;
}
#form>input[type=text]{
width:250px; margin-bottom:10px;
}
#form>#start{
margin-right:7px;
}
#form>#btn{
display:block; margin:0 auto;
}
&#13;
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<title>TimeSpan</title>
<link type='text/css' rel='stylesheet' href='external.css' />
<script type='text/javascript' src='external.js'></script>
</head>
<body>
<div class='main'>
<form id='form'>
<label for='start'>Start:</label><input id='start' name='start' type='text' />
<label for='stop'>Stop:</label><input id='stop' name='stop' type='text' />
<input id='btn' type='button' value='TimeSpan' />
</form>
<div id='output'></div>
</div>
</body>
</html>
&#13;
请注意,JavaScript在使用小数时遇到问题。解决方案是通过乘以10的幂然后除以相同的值来使它们成为整数。
答案 2 :(得分:-1)
10:57:11 + 2小时22分= 13:19:11而不是13:19:50
另外2.3775小时是120分钟+(60 * 0.3775)分钟= 22.65分钟。
所以计算是正确的,你错过了0.65分钟(39秒)
您可以使用(8559000 / 1000) mod 60
var total = (endDate.getTime() - startDate.getTime()) / 1000;
var seconds = total % 60;
total = total - seconds; // Floor to the minutes.
Now the total contains exactly 2:22 minutes (in seconds)