JS date plus minus, date operation code _ time Date

Source: Internet
Author: User
Tags error handling getdate

One, date minus days equals second date

function cc (DD,DADD) {
//can be added error handling
var a = new Date (dd)
a = a.valueof ()
a = a-dadd * * * * * 1000
   a = new Date (a)
alert (a.getfullyear () + "year" + (A.getmonth () + 1) + "month" + a.getdate () + "Day")
}
cc ("12/23/200 2 ", 2)

Here we have to add, waste a lot of time to learn:
Javascript's code for TIME
0-11 number Expression January-December: Var a= new Date (2006,5,6) turns out to be 2006-6-6
0-6 means week
1-31 means date
0-23 hours
0-59 minutes, seconds.

Two. Two-date difference (D1-D2).

function DateDiff (D1,D2) {var day = * * *
  *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

Third, the application:

<script language= "javascript" type= "Text/javascript" > var flag = Array ();//global variable var start = "2009-01-01";
var end = "2009-02-24"; 

Setflag (Start,end);

for (var i=0; i<flag.length; i++) {document.write (flag[i]+ "\n\r");} Set Date (array) function Setflag (start,end) {var cdate = array (); CDate = Start.split ("-"); var cd = cdate[1]+ "/" +cdate[2]+ 
"/" +cdate[0];
var daynum = DateDiff (End,start); for (var i=0; i<=daynum; i++) {Flag.push (AddDays (Cd,i));}
End fun//date plus days after the new date.
  function AddDays (date,days) {var nd = new Date (date);
  nd = nd.valueof ();
  nd = nd + days * 24 * 60 * 60 * 1000;
  nd = new Date (ND);
Alert (nd.getfullyear () + "year" + (Nd.getmonth () + 1) + "month" + nd.getdate () + "Day");
var y = nd.getfullyear ();
var m = nd.getmonth () +1;
var d = nd.getdate ();
if (M <= 9) m = "0" +M; 
if (d <= 9) d = "0" +D;
var cdate = y+ "-" +m+ "-" +D;
return cdate;
}//Two-date difference (D1-D2).
 function DateDiff (D1,D2) {var day = *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 </script>

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.