Js date difference days we generally use the year, month, and day for addition and subtraction can be calculated, of course, you can also convert the date into a day millisecond and then add and subtract to convert, Next let's look at some examples.
1. Calculate the number of days when the two dates are different.
The Code is as follows: |
Copy code |
Var s = (new Date (paramObj. end_date.replace (/-/g, "/")-(new Date (paramObj. start_date.replace (/-/g ,"/")))); Var day = s/1000/60/60/24
|
Example 2
Js inside can be directly Date1-Date2, and still converted to millisecond time difference calculation.
The Code is as follows: |
Copy code |
// Calculate the number of days between two dates Function DateDiff (sDate1, sDate2) {// The format of sDate1 and sDate2 is 2002-12-18 Var aDate, oDate1, oDate2, iDays ADate = sDate1.split ("-") ODate1 = new Date (aDate [1] + '-' + aDate [2] + '-' + aDate [0]) // convert to the 12-18-2002 format ADate = sDate2.split ("-") ODate2 = new Date (aDate [1] + '-' + aDate [2] + '-' + aDate [0]) IDays = parseInt (Math. abs (oDate1-oDate2)/1000/60/60/24) // converts the number of milliseconds to the number of days Return iDays }
|
2. Calculate the date after a certain number of days
In JavaScript, calculate the date of the day after the day. It is far from convenient in. Net, and a function can solve the problem. This problem has plagued me for a while and finally solved the problem through a netizen's introduction. Post it and share it with you.
The Code is as follows: |
Copy code |
<Script language = "javascript" type = "text/javascript"> Var startDate = new Date (); var intValue = 0; Var endDate = null; IntValue = startDate. getTime (); intValue + = 100*(24*3600*1000 ); EndDate = new Date (intValue ); Alert (endDate. getFullYear () + "-" + (endDate. getMonth () + 1) + "-" + endDate. getDate ()); </Script>
|
The above 100 represents the date after 100 days. You can modify it. In JS, Date. getTime () can only be the Date after 1970.01.01, and the month is 0-11, which is a little different. Don't worry. Of course, you can also calculate the date after a specific date.
The Code is as follows: |
Copy code |
<Script language = "javascript" type = "text/javascript"> Var startDate = new Date (2007, (8-1), 1, 10, 10 ); Var intValue = 0; Var endDate = null; IntValue = startDate. getTime (); intValue + = 100*(24*3600*1000 ); EndDate = new Date (intValue ); Alert (endDate. getFullYear () + "-" + (endDate. getMonth () + 1) + "-" + endDate. getDate ()); </Script>
|
If none of the above calculate the leap year, the following instance is complete.
The Code is as follows: |
Copy code |
// Determine whether it is a leap year Function isLeapYear (year ){ If (year % 4 = 0 & (year % 100! = 0) | (year % 400 = 0 ))) { Return true; } Return false; } // Judge the two dates before and after Function validatePeriod (fyear, fmonth, fday, byear, bmonth, bday ){ If (fyear <byear ){ Return true; } Else if (fyear = byear ){ If (fmonth <bmonth ){ Return true; } Else if (fmonth = bmonth ){ If (fday <= bday ){ Return true; } Else { Return false; } } Else { Return false; } } Else { Return false; } } // Calculate the difference between the two dates Function dateDiff (d1, d2 ){ Var disNum = compareDate (d1, d2 ); Return disNum; } Function compareDate (date1, date2) { Var regexp =/^ (d {1, 4}) [-|.] {1} (d {1, 2}) [-|.] {1} (d {1, 2}) $ /; Var monthDays = [,]; Regexp. test (date1 ); Var date1Year = RegExp. $1; Var date1Month = RegExp. $2; Var date1Day = RegExp. $3; Regexp. test (date2 ); Var date2Year = RegExp. $1; Var date2Month = RegExp. $2; Var date2Day = RegExp. $3; If (validatePeriod (date1Year, date1Month, date1Day, date2Year, date2Month, date2Day )){ FirstDate = new Date (date1Year, date1Month, date1Day ); SecondDate = new Date (date2Year, date2Month, date2Day ); Result = Math. floor (secondDate. getTime ()-firstDate. getTime ()/(1000*3600*24 )); For (j = date1Year; j <= date2Year; j ++ ){ If (isLeapYear (j )){ MonthDays [1] = 2; } Else { MonthDays [1] = 3; } For (I = date1Month-1; I <date2Month; I ++ ){ Result = result-monthDays [I]; } } Return result; } Else { Alert ('Sorry, the first time must be earlier than the second time. Thank you! '); Exit; } }
Call this function to pass two time values: 2013-01-19 2013-12-19 Days = dateDiff (d1, d2 ); |