简体   繁体   中英

setDate with getDate + n returns today's date + n

I want d2 to be d1 + 14 days, instead it's todays' date + 14 days.

<script type="text/javascript">
  var d1 = new Date(2014, 7, 1);
  var d2 = new Date();
  d2.setDate(d1.getDate() + 14);

  // d1 = Fri Aug 01 2014 00:00:00 GMT+0100 (GMT Daylight Time)
  document.write("d1 = " + d1);
  document.write("<br></br>")

  // today's date is 16-04-2014
  // d2 = Tue Apr 15 2014 17:36:03 GMT+0100 (GMT Daylight Time)
  document.write("d2 = " + d2);
</script>

How can I make d2 = d1 + 14 days?

setDate , somewhat surprisingly, sets the day-of-the-month of the existing instance. Since you've initialized d2 with today, you're just changing the day-of-month for that date, not taking on any other aspect of d1 .

You probably want:

var d1 = new Date(2014, 7, 1);  // A specific date
var d2 = new Date(d1);          // Clone that date
d2.setDate(d1.getDate() + 14);  // Move forward 14 days

That gives us August 1st for d1 , and August 15th for d2 .

Another option is:

var d1 = new Date(2014, 7, 1);
var d2 = new Date(d1.getFullYear(), d1.getMonth(), d1.getDate() + 14);

Both of those work even when you're going past the end of the month, btw.

Complete example of the second option: Live Copy

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Date plus 14 days</title>
  <style>
    body {
      font-family: sans-serif;
    }
  </style>
</head>
<body>
  <script>
    (function() {
      var d1, d2;

      display("Starting with August 1st:")
      d1 = new Date(2014, 7, 1);
      d2 = new Date(d1.getFullYear(), d1.getMonth(), d1.getDate() + 14);
      display("d1 = " + d1);
      display("d1 = " + d2);
      document.body.appendChild(document.createElement('hr'));

      display("Starting with August 20th:")
      d1 = new Date(2014, 7, 20);
      d2 = new Date(d1.getFullYear(), d1.getMonth(), d1.getDate() + 14);
      display("d1 = " + d1);
      display("d1 = " + d2);

      function display(msg) {
        var p = document.createElement('p');
        p.innerHTML = String(msg);
        document.body.appendChild(p);
      }
    })();
  </script>
</body>
</html>

You're making a new Date (today) for d2 but only changing the day, this should work:

var d1 = new Date(2014, 7, 1);
var d2 = new Date(2014, 7, d1.getDate()+14);

Only put getTime() function inside d2 Date() function,

Check this Demo jsFiddle

JavaScript

  var d1 = new Date(2014, 7, 1);
  var d2 = new Date(d1.getTime());
  d2.setDate(d1.getDate() + 14);

  // d1 = Fri Aug 01 2014 00:00:00 GMT+0100 (GMT Daylight Time)
  document.write("d1 = " + d1);
  document.write("<br></br>")

  // today's date is 16-04-2014
  // d2 = Tue Apr 15 2014 17:36:03 GMT+0100 (GMT Daylight Time)
  document.write("d2 = " + d2);

Result

d1 = Fri Aug 1 00:00:00 UTC+0530 2014

d2 = Fri Aug 15 00:00:00 UTC+0530 2014

d1 is a date in August, d2 is today. You are setting d2 to today + 14 days. The proper result is below:

var d1 = new Date(2014, 7, 1);
var d2 = new Date(d1.setDate(d1.getDate() + 12));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM