简体   繁体   English

JavaScript添加天数不增加月份

[英]JavaScript add days not incrementing the month

I know this question has come up many times and I have used the recommended solution to created my function, but I have used it to scroll a calendar along the next/prev 4 weeks. 我知道这个问题已经出现很多次了,我使用了推荐的解决方案来创建我的函数,但是我使用它来在下一个/上一个4周滚动日历。

What seems to happen is that the first time it calculates d + 28 it is correct, but when the outputted result is used in the function it does not work. 似乎发生的事情是,它第一次计算d + 28时是正确的,但是当在函数中使用输出结果时,它将不起作用。

Examples from console log: 控制台日志中的示例:

Press 'next' first time: 第一次按“下一步”:

start Before = Tue Jul 28 2015 21:53:20 GMT+0100 (GMT Daylight Time)
start After = Tue Aug 25 2015 21:53:22 GMT+0100 (GMT Daylight Time)

Press 'next' a second time: 再次按“下一步”:

start Before = Tue Aug 25 2015 21:53:22 GMT+0100 (GMT Daylight Time)
start After = Sat Aug 22 2015 21:53:28 GMT+0100 (GMT Daylight Time)

and so it goes on. 所以它继续。

HTML: HTML:

 <!DOCTYPE html>
 <html ng-app="testApp">
 <head>
  <script src="https://code.angularjs.org/1.4.2/angular.js" data-require="angular.js@1.4.2" data-semver="1.4.2"></script>
  <link href="style.css" rel="stylesheet" />
  <script src="script.js"></script>
 </head>
 <body ng-controller="testController">
  <h1>Hello Plunker!</h1>
  <input type="button" ng-click="prevWeeks(start)" value="subtract 28 days" ng-model="start" />
  <input type="button" ng-click="nextWeeks(start)" value="add 28 days" ng-model="start" />
  <hr />
  <div>{{start}}</div>
 </body>
 </html>

JS: JS:

var testApp = angular.module('testApp', []);

testApp.controller('testController', function($scope) {
  $scope.start = new Date();

  $scope.nextWeeks = function(d) {
    console.log('Next:');
    console.log('  start Before = ' + d);
    $scope.start = addDays(d, 28);
    console.log('    start After = ' + $scope.start);
    console.log('\n');
    }

  $scope.prevWeeks = function(d) {
    console.log('Previous');
    console.log('  start Before = ' + d);
    $scope.start = addDays(d, -28);
    console.log('    start After = ' + d);
    console.log('\n');
  }

});

function addDays(d, qty) {
  var dd = d.getDate();
  var mm = d.getMonth() + 1;
  var yyyy = d.getFullYear();
  if (dd < 10) dd = '0' + dd;
  if (mm < 10) mm = '0' + mm;
  var moreDate = new Date(yyyy, mm - 1, dd);

  var someDate = new Date();
  someDate.setDate(moreDate.getDate() + qty);

  return someDate;
}

On Plnkr here . 这里 Plnkr。

Any ideas? 有任何想法吗? Have I slipped up somewhere? 我在某个地方溜了吗?

Your function can be simplified greatly, because the Date type automatically takes care of adjusting the month when the date goes out of range. 您的功能可以大大简化,因为当日期超出范围时, Date类型会自动调整月份。

function addDays(d, qty) {
    var dd = d.getDate();
    var mm = d.getMonth();
    var yyyy = d.getFullYear();
    return new Date(yyyy, mm, dd + qty);
}

Another way would be: 另一种方法是:

function addDays(d, qty) {
    var newDate = new Date(d.getTime()); // Copy date
    newDate.setDate(d.getDate() + qty);
    return newDate;
}

Since ways to fix this has already been provided let me tell you why your way doesn't work. 由于已经提供了解决此问题的方法,所以让我告诉您为什么您的方法行不通。 You first create moreDate which adjusts the year and month to the intended values but after that you create someDate which is initialized as the current day with current month and current year. 首先,您需要创建moreDate来将年和月调整为预期值,但是之后您将创建someDate ,该someDate将被初始化为具有当前月份和当前年份的当前日期。 You add qty to the date and set that on someDate . 您将qty添加到日期并将其设置为someDate The calculation then is relative to today and not relative to the date provided as argument. 然后,该计算是相对于今天,而不是相对于作为参数提供的日期。 For example, 例如,

var d = new Date(); //July 28 2015
d = addDays(d,28); //Aug 25 2015,works because we added 28 days to today
d = addDays(d,28); //Aug 22 2015!! Looks wrong, but is it?

The last call clearly looks wrong but it isn't really. 最后一个电话显然看起来是错误的,但实际上并非如此。 Stepping through your code: 逐步执行代码:

  /*more code*/
  var moreDate = new Date(yyyy, mm - 1, dd); //Aug 25 2015

  var someDate = new Date(); //Catch!! July 28 2015
  someDate.setDate(moreDate.getDate() + qty); //Catch!!
  //the call above added 28+25 days to today which is Aug 22 2015
  return someDate;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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