簡體   English   中英

JavaScript添加天數不增加月份

[英]JavaScript add days not incrementing the month

我知道這個問題已經出現很多次了,我使用了推薦的解決方案來創建我的函數,但是我使用它來在下一個/上一個4周滾動日歷。

似乎發生的事情是,它第一次計算d + 28時是正確的,但是當在函數中使用輸出結果時,它將不起作用。

控制台日志中的示例:

第一次按“下一步”:

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)

再次按“下一步”:

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)

所以它繼續。

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:

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;
}

這里 Plnkr。

有任何想法嗎? 我在某個地方溜了嗎?

您的功能可以大大簡化,因為當日期超出范圍時, Date類型會自動調整月份。

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

另一種方法是:

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

由於已經提供了解決此問題的方法,所以讓我告訴您為什么您的方法行不通。 首先,您需要創建moreDate來將年和月調整為預期值,但是之后您將創建someDate ,該someDate將被初始化為具有當前月份和當前年份的當前日期。 您將qty添加到日期並將其設置為someDate 然后,該計算是相對於今天,而不是相對於作為參數提供的日期。 例如,

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?

最后一個電話顯然看起來是錯誤的,但實際上並非如此。 逐步執行代碼:

  /*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