SoFunction
Updated on 2025-03-10

JS date addition and subtraction, date operation code

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

function cc(dd,dadd){
//Error processing can be addedvar a = new Date(dd)
a = ()
a = a - dadd * 24 * 60 * 60 * 1000
a = new Date(a)
alert(() + "Year" + (() + 1) + "moon" + () + "day")
}
cc("12/23/2002",2)

I have to add it here, wasting a lot of time to learn lessons:
Javascript code for time
0-11 Numbers represent January-December: var a= new Date(2006,5,6) The result is 2006-6-6
0-6 means week
1-31 indicates 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 = ("-");
  var checkDate = new Date();
    (dateArr[0], dateArr[1]-1, dateArr[2]);
  var checkTime = ();
 
  var dateArr2 = ("-");
  var checkDate2 = new Date();
    (dateArr2[0], dateArr2[1]-1, dateArr2[2]);
  var checkTime2 = ();
  
  var cha = (checkTime - checkTime2)/day; 
    return cha;
  }catch(e){
  return false;
}
}//end fun

III. Application:

<script language="javascript" type="text/javascript">
var flag = Array();//Global variables
var start = "2009-01-01";
var end = "2009-02-24";
SetFlag(start,end); 

for(var i=0; i<; i++){
  (flag[i]+"\n\r");
}

//Set the date (array) within the cyclefunction SetFlag(start,end){
var cdate = Array();
cdate = ("-");
var cd = cdate[1]+"/"+cdate[2]+"/"+cdate[0]; 
var dayNum = DateDiff(end,start);
for(var i=0; i<=dayNum; i++){
  (AddDays(cd,i));
}
}//end fun
//The new date after the date plus the number of days.function AddDays(date,days){
var nd = new Date(date);
  nd = ();
  nd = nd + days * 24 * 60 * 60 * 1000;
  nd = new Date(nd);
  //alert(() + "year" + (() + 1) + "month" + () + "day");var y = ();
var m = ()+1;
var d = ();
if(m <= 9) m = "0"+m;
if(d <= 9) d = "0"+d; 
var cdate = y+"-"+m+"-"+d;
return cdate;
}
//The difference between two dates (d1 - d2).function DateDiff(d1,d2){
  var day = 24 * 60 * 60 *1000;
try{  
    var dateArr = ("-");
  var checkDate = new Date();
    (dateArr[0], dateArr[1]-1, dateArr[2]);
  var checkTime = ();
 
  var dateArr2 = ("-");
  var checkDate2 = new Date();
    (dateArr2[0], dateArr2[1]-1, dateArr2[2]);
  var checkTime2 = ();
  
  var cha = (checkTime - checkTime2)/day; 
    return cha;
  }catch(e){
  return false;
}
}//end fun
</script>