Calculate timespan in JavaScript

前端 未结 7 2064
刺人心
刺人心 2021-01-01 21:48

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

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

txtEnd.Text = 09/19/200

7条回答
  •  青春惊慌失措
    2021-01-01 21:50

    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.

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

提交回复
热议问题