计算JavaScript中的时间跨度
我有一个带有开始时间和结束时间文本框的.net 2.0 ascx控件。数据如下:
txtStart.Text = 09/19/2008 07:00:00
txtEnd.Text = 09/19/2008 05:00:00
我想用JavaScript计算总时间(小时和分钟),然后将其显示在页面上的文本框中。
解决方案
一旦预先知道了文本框的日期格式,就可以使用Javascript中的Matt Kruse的Date函数将两者转换为时间戳,然后减去然后写入结果文本框。
同样,可以针对目的调整stringToDate的JQuery Date输入代码,以下内容以" YYYY-MM-DD"格式的字符串并将其转换为日期对象。这些对象的时间戳(getTime())可用于计算。
stringToDate: function(string) {
var matches;
if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
return new Date(matches[1], matches[2] - 1, matches[3]);
} else {
return null;
};
}
我接受了@PConroy所做的工作,并通过为我们进行计算将其添加到其中。我还添加了正则表达式以确保时间是创建日期对象的字符串的一部分。
<html>
<head>
<script type="text/javascript">
function stringToDate(string) {
var matches;
if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2}) (\d{2,2}):(\d{2,2}):(\d{2,2})$/)) {
return new Date(matches[1], matches[2] - 1, matches[3], matches[4], matches[5], matches[6]);
} else {
return null;
};
}
//Convert duration from milliseconds to 0000:00:00.00 format
function MillisecondsToDuration(n) {
var hms = "";
var dtm = new Date();
dtm.setTime(n);
var h = "000" + Math.floor(n / 3600000);
var m = "0" + dtm.getMinutes();
var s = "0" + dtm.getSeconds();
var cs = "0" + Math.round(dtm.getMilliseconds() / 10);
hms = h.substr(h.length-4) + ":" + m.substr(m.length-2) + ":";
hms += s.substr(s.length-2) + "." + cs.substr(cs.length-2);
return hms;
}
var beginDate = stringToDate('2008-09-19 07:14:00');
var endDate = stringToDate('2008-09-19 17:35:00');
var n = endDate.getTime() - beginDate.getTime();
alert(MillisecondsToDuration(n));
</script>
</head>
<body>
</body>
</html>
这很粗糙,因为我很快就对其进行了编码,但是它可以工作。我测试了一下。警报框将显示0010:21:00.00(HHHH:MM:SS.SS)。基本上,我们需要做的就是从文本框中获取值。
最重要的答案是假定使用字符串操作。这是适用于纯日期对象的解决方案:
var start = new Date().getTime();
window.setTimeout(function(){
var diff = new Date(new Date().getTime() - start);
// this will log 0 hours, 0 minutes, 1 second
console.log(diff.getHours(), diff.getMinutes(),diff.getSeconds());
},1000);
function stringToDate(string) {
var matches;
if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2}) (\d{2,2}):(\d{2,2}):(\d{2,2})$/)) {
return new Date(matches[1], matches[2] - 1, matches[3], matches[4], matches[5], matches[6]);
} else {
return null;
};
}
function getTimeSpan(ticks) {
var d = new Date(ticks);
return {
hour: d.getUTCHours(),
minute: d.getMinutes(),
second: d.getSeconds()
}
}
var beginDate = stringToDate('2008-09-19 07:14:00');
var endDate = stringToDate('2008-09-19 17:35:00');
var sp = getTimeSpan(endDate - beginDate);
alert("timeuse:" + sp.hour + " hour " + sp.minute + " minute " + sp.second + " second ");
我们可以使用getUTCHours()代替Math.floor(n / 3600000);
使用Math.floor(n / 3600000)代替getUTCHours(),否则我们将损失大于24小时的小时数。
例如,如果我们有126980000毫秒,则应转换为0035:16:20.00
如果使用getUTCHours(),则会得到不正确的字符串0011:16:20.00
最好改用以下代码(由KK-MOD表示的修改):
function MillisecondsToDuration(n) {
var hms = "";
var dtm = new Date();
dtm.setTime(n);
var d = Math.floor(n / 3600000 / 24); // KK-MOD
var h = "0" + (Math.floor(n / 3600000) - (d * 24)); // KK-MOD
var m = "0" + dtm.getMinutes();
var s = "0" + dtm.getSeconds();
var cs = "0" + Math.round(dtm.getMilliseconds() / 10);
hms = (d > 0 ? d + "T" : "") + h.substr(h.length - 2) + ":" + m.substr(m.length - 2) + ":"; // KK-MOD
hms += s.substr(s.length - 2) + "." + cs.substr(cs.length - 2);
return hms; }
因此,现在192680000显示为1T11:16:20.00,即1天11小时16分20秒

