Ron*_*fca 14 javascript asp.net
我有一个带有开始时间和结束时间文本框的.net 2.0 ascx控件.数据如下:
txtStart.Text = 09/19/2008 07:00:00
txtEnd.Text = 09/19/2008 05:00:00
我想用JavaScript计算总时间(小时和分钟),然后在页面上的文本框中显示它.
小智 6
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 ");
Run Code Online (Sandbox Code Playgroud)
你可以使用getUTCHours()代替Math.floor(n/3600000);
一旦您的文本框日期格式提前知道,您就可以使用 在Javascript中 Matt Kruse的日期函数将两者转换为时间戳,减去然后写入结果文本框.
同样,JQuery日期输入代码stringToDate可以适用于您的目的 - 下面采用格式为"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;
};
}
Run Code Online (Sandbox Code Playgroud)