0

Angular UI の Datepicker を、変数として渡された 2 つの日付の間に制限したいと考えています。これは、この問題の作業プランカーです: http://plnkr.co/edit/zsjpoVZtHqJLIP2RW6vm?p=preview

変数は次のとおりです。

mycurrentdate = '2016-04-15'
mymindate = '2016-04-01'
mymaxmonth = '2016-05-01'

実際の最大日付は mymaxmonth の終わりになるため、この場合は「2016-05-31」です。

$scope.maxDate = new Date(
                    $scope.mymaxmonth + (TO THE END OF THE MONTH)
                );

注意すべきことの 1 つは、実行するとnew Date()、指定された日付の前日の日付が返されることです。例えば:

$scope.minDate = new Date(
                    $scope.mymindate
                );

$scope.minDate が返される場所Wed Mar 30 2016 17:00:00 GMT-0700 (PDT)4 月 1 日ではなく 3 月 30 日が返される理由を調べたところ、タイムゾーン エラーのように見えますか?

mymindate を '2016-04-01' に設定し、mymaxdate = '2016-05-31' を取得して、この範囲外のすべての日付を無効にしたいと考えています。どうすれば月末にたどり着くことができるか、特に頭を悩ませています。javascriptにendofmonth()関数はありますか?

私が持っているコントローラで:

$scope.mymindate = '2016-04-01';
$scope.mymaxmonth = '2016-05-01'; //want mymaxdate to be '2016-05-31'

$scope.minDate = new Date(
  $scope.mymindate
  );
$scope.maxDate = new Date(
  $scope.mymaxmonth + 1
  );

私が持っているテンプレートでは:

<p class="input-group">
          <input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt" is-open="popup1.opened" min="minDate" max="maxDate" datepicker-options="dateOptions" ng-required="true" close-text="Close" alt-input-formats="altInputFormats" />
          <span class="input-group-btn">
            <button type="button" class="btn btn-default" ng-click="open1()"><i class="glyphicon glyphicon-calendar"></i></button>
          </span>
        </p>
4

1 に答える 1

0

Plunkr の例は、書かれているとおりには機能しません。

バニラ Javascript を使用して Javascript の日付を適切に操作する方法について読むことから始めることをお勧めします。

何をどのように操作したいか (この場合は月) がわかったら、MomentJS などの簡単に操作できるライブラリをお勧めします

Moment を使用すると、任意の日付を取得し、追加、月末、ローカル (タイムゾーン修正) など、ほぼすべての方法で操作できます。

任意の日付を amoment()でラップし、MomentJS ドキュメントを参照して、必要なことを行うことができます。new Date()Angular UI の Datepicker で動作させるには、日付を でラップする必要があります。

new Date(moment($scope.mymaxmonth).add(1,'months')); //Fri Jun 1 2016 16:41:02 GMT-0700 (Pacific Daylight Time)
new Date(moment($scope.mymaxmonth).endOf('month')); // Tue May 31 2016 23:59:59 GMT-0700 (Pacific Daylight Time)
new Date(moment($scope.mymindate).local()); //Fri Apr 01 2016 00:00:00 GMT-0700 (Pacific Daylight Time)

Moment を Angular アプリに統合する方法をいくつか紹介ます

于 2016-04-15T23:49:18.667 に答える