【问题标题】:Getting current date in Angular4在Angular4中获取当前日期
【发布时间】:2018-04-20 10:49:44
【问题描述】:

我已经使用输入文本框选择日历和时间

[(ngModel)]="toDateTransaction" useValueAsDate

现在我想将所选日期与当前日期(今天的日期)进行比较。如何得到那个。我用过

let currentDate = Date.now(); 

但它给出了奇怪的输出。我想在 angular4 和 node.js 中使用

【问题讨论】:

    标签: node.js angular angular4-forms


    【解决方案1】:

    如果您使用[] 语法对ngModel 进行单向绑定,则在组件类中更改域模型的值将在视图中设置该值。如果您使用[(ngModel)] 语法(也称为“香蕉盒语法”)进行双向绑定,则 UI 中的值也将始终同步回您的类中的域模型。
    [(ngModel)] 语法只能设置数据绑定属性。如果您需要做更多或不同的事情,您可以编写扩展表格 ngModel 数据属性设置元素的值属性,ngModelChange 事件属性监听元素值的变化
    HTML:

    <input type="date" #ref [ngModel]="toDateTransaction" (ngModelChange)="onCompareDate(ref.value)">
    

    组件:

    onCompareDate(olddate):boolean{
        let date1 = new Date(olddate);
        let date2 = new Date();//your logic
              return date1.getFullYear() === date2.getFullYear() && date1.getMonth() === date2.getMonth() && date1.getDate() === date2.getDate();
    }
    

    【讨论】:

      【解决方案2】:

      你像这样比较日期(示例):

      date1 = new Date();
      date2 = new Date(date1);
      
      date1.getDate() === date2.getDate()
      

      【讨论】:

        【解决方案3】:

        这应该可以解决您的问题,前提是我完全清楚您的问题。

           angular.module('dateInputExample', [])
             .controller('DateController', ['$scope', function($scope) {
               $scope.example = {
                 value: new Date()
               };
             }]);
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
        <form name="myForm" ng-controller="DateController as dateCtrl">
           <label for="exampleInput">Pick a date:</label>
           <input type="date" id="exampleInput" name="input" ng-model="toDateTransaction"
               placeholder="yyyy-MM-dd" min="2018-01-01" max="2025-12-31" required />
        </form>

        您可以阅读更多表格ngModel

        【讨论】:

          猜你喜欢
          • 2023-02-08
          • 2015-04-28
          • 2012-07-30
          • 2019-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多