Javascript time difference calculation function sharing _ javascript tips-js tutorial

Source: Internet
Author: User
Obtain the time difference, in the format of year-month-day hour: minute: Second or year-month-day hour: minute: Second The Code is as follows:


/*
* Time Difference obtained, in the format of year-month-day hour: minute: Second or year/month/day hour: minute: Second
* The year, month, and day are in full format, for example, 01:00:00
* Return accuracy: seconds, minutes, hours, days
*/
Function GetDateDiff (startTime, endTime, diffType ){
// Convert the time format of xxxx-xx to the format of xxxx/xx
StartTime = startTime. replace (/\-/g ,"/");
EndTime = endTime. replace (/\-/g ,"/");
// Converts the computing interval class characters to lowercase letters.
DiffType = diffType. toLowerCase ();
Var sTime = new Date (startTime); // start time
Var eTime = new Date (endTime); // End Time
// Number used as the Divisor
Var pNum = 1;
Switch (diffType ){
Case "second ":
Pnum= 1000;
Break;
Case "minute ":
Pnum= 1000*60;
Break;
Case "hour ":
Pnum= 1000*3600;
Break;
Case "day ":
PNum = 1000*3600*24;
Break;
Default:
Break;
}
Return parseInt (eTime. getTime ()-sTime. getTime ()/parseInt (pNum ));
}


The call method is also simple:
GetDateDiff ("00:00:00", "00:01:40", "day ")
This is the computing day
GetDateDiff ("00:00:00", "00:01:40", "seond") is calculated in seconds.
Related Article

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.