Home  >  Article  >  Web Front-end  >  js example code for calculating the difference in days between two times_javascript skills

js example code for calculating the difference in days between two times_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:14:081232browse

Copy code The code is as follows:

//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 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=[0,3,0,1,0,1,0 ,0,1,0,0,1];
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 result=result-monthDays[i];
}
}
return result;
}else{
alert('Sorry for the first one The time must be less 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);

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn