Home  >  Article  >  Web Front-end  >  JS date addition and subtraction, date operation code_time and date

JS date addition and subtraction, date operation code_time and date

WBOY
WBOYOriginal
2016-05-16 15:33:401438browse

1. The date minus the number of days equals the second date

function cc(dd,dadd){
//可以加上错误处理
var a = new Date(dd)
a = a.valueOf()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(a)
alert(a.getFullYear() + "年" + (a.getMonth() + 1) + "月" + a.getDate() + "日")
}
cc("12/23/2002",2)

I have to make supplements here, wasting a lot of time to draw lessons:
Javascript code for time
Numbers 0-11 represent January-December: var a= new Date(2006,5,6) The result is 2006-6-6
0-6 means day of the week
1-31 represents the date
0-23 hours
0-59 minutes, seconds

Two. //The difference between two dates (d1 - d2).

function DateDiff(d1,d2){
  var day = 24 * 60 * 60 *1000;
try{  
    var dateArr = d1.split("-");
  var checkDate = new Date();
    checkDate.setFullYear(dateArr[0], dateArr[1]-1, dateArr[2]);
  var checkTime = checkDate.getTime();
 
  var dateArr2 = d2.split("-");
  var checkDate2 = new Date();
    checkDate2.setFullYear(dateArr2[0], dateArr2[1]-1, dateArr2[2]);
  var checkTime2 = checkDate2.getTime();
  
  var cha = (checkTime - checkTime2)/day; 
    return cha;
  }catch(e){
  return false;
}
}//end fun

3. Application:

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