在JavaScript中计算时间跨度 [英] Calculate timespan in JavaScript

查看:193
本文介绍了在JavaScript中计算时间跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个开始时间和结束时间文本框的.NET 2.0 ascx控件。数据如下:

I have a .net 2.0 ascx control with a start time and end time textboxes. The data is as follows:

txtStart.Text = 2008年9月19日07:00:00

txtStart.Text = 09/19/2008 07:00:00

txtEnd.Text = 2008年9月19日05:00:00

txtEnd.Text = 09/19/2008 05:00:00

我想在JavaScript来计算总时间(小时和分钟),然后在页面上的文本框显示。

I would like to calculate the total time (hours and minutes) in JavaScript then display it in a textbox on the page.

推荐答案

在你的文本日期格式是事先知道,你可以使用的马特·克鲁斯的日期函数在Javascript 的两个转换为一个时间戳,减去然后写入结果文本框。

Once your textbox date formats are known in advance, you can use Matt Kruse's Date functions in Javascript to convert the two to a timestamp, subtract and then write to the resulting text box.

同样的 JQuery的日期输入 code为 stringToDate 可以适应你的目的 - 下面需要一个字符串格式为YYYY-MM-DD,并将其转换为Date对象。时间戳(的getTime()),这些对象可用于您的计算。的

Equally the JQuery Date Input code for stringToDate could be adapted for your purposes - the below takes a string in the format "YYYY-MM-DD" and converts it to a date object. The timestamp (getTime()) of these objects could be used for your calculations.

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;
    };
}

这篇关于在JavaScript中计算时间跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆