How JavaScript calculates the time difference between two days

Source: Internet
Author: User

Sometimes we need to know how many days between two dates, how many hours, or even how many minutes in seconds. Here we use JavaScript to implement a function to calculate the time difference of two dates, first look at the code:

01 <script type="text/javascript">
02
03 /**
04 * 时间对象的格式化;
05 */
06 Date.prototype.format = function(format){
07  /*
08   * eg:format="YYYY-MM-dd hh:mm:ss";
09   */
10     varo = {
11         "M+":  this.getMonth()+1,  //month
12         "d+":  this.getDate(),     //day
13         "h+":  this.getHours(),    //hour
14         "m+":  this.getMinutes(),  //minute
15         "s+":  this.getSeconds(), //second
16         "q+":  Math.floor((this.getMonth()+3)/3),  //quarter
17         "S":  this.getMilliseconds() //millisecond
18     }
19   
20     if(/(y+)/.test(format)) {
21         format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
22     }
23  
24     for(varino) {
25         if(newRegExp("("+ k +")").test(format)) {
26             format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
27         }
28     }
29     returnformat;
30 }
31
32
33 /*
34 * 获得时间差,时间格式为 年-月-日 小时:分钟:秒 或者 年/月/日 小时:分钟:秒
35 * 其中,年月日为全格式,例如 : 2010-10-12 01:00:00
36 * 返回精度为:秒,分,小时,天
37 */
38
39 functionGetDateDiff(startTime, endTime, diffType) {
40     //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式
41     startTime = startTime.replace(/\-/g, "/");
42     endTime = endTime.replace(/\-/g, "/");
43
44     //将计算间隔类性字符转换为小写
45     diffType = diffType.toLowerCase();
46     varsTime = newDate(startTime);      //开始时间
47     vareTime = newDate(endTime);  //结束时间
48     //作为除数的数字
49     vardivNum = 1;
50     switch(diffType) {
51         case"second":
52             divNum = 1000;
53             break;
54         case"minute":
55             divNum = 1000 * 60;
56             break;
57         case"hour":
58             divNum = 1000 * 3600;
59             break;
60         case"day":
61             divNum = 1000 * 3600 * 24;
62             break;
63         default:
64             break;
65     }
66     returnparseInt((eTime.getTime() - sTime.getTime()) / parseInt(divNum));
67 }
68
69 vartestDate = newDate();
70 vartestStr = testDate.format("yyyy-MM-dd hh:mm:ss");
71
72 varresult = GetDateDiff("2010-02-26 16:00:00", testStr, "day");
73 document.write("简明现代魔法 www.nowamagic.net 建站已有"+ result + "天了。");
74 //alert(result);
75 </script>

The method used is simple, such as calculating the number of days to do this:

1 GetDateDiff("2010-02-26 16:00:00""2011-07-02 21:48:40""day");

The number of seconds can be calculated as follows:

1 GetDateDiff("2010-02-26 16:00:00""2011-07-02 21:48:40""second");

The operating result of the instance program is as follows: Guangde County Energy Bureau

The Concise modern Magic Www.nowamagic.net station has been built for 1695 days.

How JavaScript calculates the time difference between two days

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.