JavaScript function sharing _javascript techniques for calculating time lag

Source: Internet
Author: User
Copy code code as follows:

/*
* Get time difference, the date format is year-month-day hour: minutes: seconds or Year/month/day hours: minutes: seconds
* in which, the date of year is full format, for example: 2010-10-12 01:00:00
* Return precision: seconds, minutes, hours, days
*/
function Getdatediff (StartTime, Endtime, Difftype) {
Converts the XXXX-XX-XX time format to the XXXX/XX/XX format
StartTime = Starttime.replace (/\-/g, "/");
Endtime = Endtime.replace (/\-/g, "/");
Converts the calculated interval class character to lowercase
Difftype = Difftype.tolowercase ();
var stime = new Date (starttime); Start time
var etime = new Date (endtime); End time
As a divisor of the number
var divnum = 1;
Switch (difftype) {
Case "Second":
Divnum = 1000;
Break
Case "Minute":
Divnum = 1000 * 60;
Break
Case "Hour":
divnum = 1000 * 3600;
Break
Case ' Day ':
divnum = 1000 * 3600 * 24;
Break
Default
Break
}
Return parseint (Etime.gettime ()-stime.gettime ())/parseint (divnum));
}

Calling a method is also simple:
Getdatediff ("2010-10-11 00:00:00", "2010-10-11 00:01:40", "Day")
This is the count of days
Getdatediff ("2010-10-11 00:00:00", "2010-10-11 00:01:40", "Seond") is the count of 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.