JS Calculation Time Difference "unit: minutes"

Source: Internet
Author: User
Tags date1
Time Format Date.prototype.Format = function (fmt) {var o = {"m+": This.getmonth () + 1, "d+": THIS.GETDA Te (), "h+": this.gethours (), "m+": This.getminutes (), "s+": This.getseconds (), "q+": Math.
    Floor ((This.getmonth () + 3)/3), "S": This.getmilliseconds ()};
    if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length)); For (var k in O) if (new RegExp ("+ K +")). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = 1)?
    (O[k]): (("+ o[k]"). substr (("" + o[k). length));
return FMT; //Calculation time difference-minutes function timedifference (time1, time2) {//Determine if start time is greater than end date if (Time1 > Time2) {alert ("Start time cannot be greater than the end time.
    return false;
    //intercept string var begin1 = time1.substr (0). Split ("-");

    var end1 = time2.substr (0). Split ("-");
 Regroup the split array and instantiate the new Date object var date1 = (Begin1[0] +-+ begin1[1] +-+ begin1[2);   var date2 = new Date (End1[0] +-+ end1[1] +-+ end1[2]);

    Gets the difference value m between two dates, in minutes var m = parseint (Math.Abs (date2-date1)/1000/60); The number of hours and minutes is added to get the total number of minutes//time1.substr (11,2) intercept a string to get the hours of Time//parseint (Time1.substr (11,2)) *60 convert hours into minutes var min1 = pa
    Rseint (TIME1.SUBSTR (2)) * + parseint (TIME1.SUBSTR (14, 2));

    var min2 = parseint (Time2.substr (one, 2)) * + parseint (TIME2.SUBSTR (14, 2));

    Subtract two minutes to get the difference of time part, var n = min2-min1 in minutes;
    var sec1 = parseint (Time1.substr (17, 2))/60;
    var sec2 = parseint (Time2.substr (17, 2))/60;

    var s = sec2-sec1;
    Add the difference value calculated by the two parts of the date and time, that is, the number of minutes after subtracting two times var minutes = m + n+s;
Return minutes.tofixed (2);
    }//time-plus minutes function addminutes (T, m) {var time = new Date (t);
    Time.setminutes (Time.getminutes () + M, Time.getseconds (), 0); return time;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.