JS date addition and subtraction, date calculation code, js date

Source: Internet
Author: User

JS date addition and subtraction, date calculation code, js date

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

Function cc (dd, dadd) {// you can add error processing var a = new Date (dd) a =. valueOf () a = a-dadd * 24*60*60 * 1000a = new Date (a) alert (. getFullYear () + "year" + (. getMonth () + 1) + "month" +. getDate () + "day")} cc ("12/23/2002", 2)

I have to add some resources here, wasting a lot of time to draw lessons:
Time code of Javascript
0-11: The number is 1-12 months. var a = new Date (, 6). The result is-6.
0-6 indicates the week
1-31 represents the date
0-23 hours
0-59 minutes, seconds

II. // 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

Iii. 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 the date (array) function SetFlag (start, end) in the cycle) {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 // the new date after the date plus the number of days. 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;} // The difference between the 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 </script>

Related Article

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.