【问题标题】:How to initialize the date value of Angular Material md-datepicker to any specified date如何将Angular Material md-datepicker的日期值初始化为任意指定日期
【发布时间】:2017-03-04 09:53:47
【问题描述】:

在 Angular Material md-datepicker 中,当我点击日期选择器时,它会将系统的当前日期设置为今天的日期。我们注意到今天的日期在日期选择器的日历中被圈出。我想知道是否有办法将任何给定日期设置为今天的日期。请参考这张图片。 md-datepicker's calender (today's date highlighted in Yellow)

所以,就我而言,如果我将今天的日期设置为 2017 年 4 月 2 日,日期选择器应将其设置为今天的日期并在日历上圈出该日期

【问题讨论】:

  • 您可以使用 并在您的控制器中 this.myDate = new Date( '2017-04-17T03:24:00');

标签: javascript angularjs angular-material


【解决方案1】:

HTML

<md-content ng-controller="AppCtrl as ctrl" layout-padding="" ng-cloak="" class="datepickerdemoBasicUsage" ng-app="MyApp">
  <div layout-gt-xs="row">
    <div flex-gt-xs="">
      <h4>Standard date-picker</h4>
      <md-datepicker ng-model="ctrl.myDate" md-placeholder="Enter date"></md-datepicker>
    </div>
</md-content>

JS

angular.module('MyApp',['ngMaterial', 'ngMessages', 'material.svgAssetsCache']).controller('AppCtrl', function() {
  this.myDate = new Date('2017-04-17T03:24:00');
  this.isOpen = false;
});

this.myDate = new Date('2017-04-17T03:24:00');

您可以在此处设置自己的日期。在您的情况下,您希望 2017 年 4 月 2 日作为日期时间选择器中的默认日期,那么您应该编写

this.myDate = new Date('2017-04-01T03:24:00');

Live Demo

我为此参考了 Angular Material 网站

【讨论】:

  • 我还是觉得这很不方便……今天呢? new Date() 记住时间细节。如果我使用 getMonth 构造 new Date("yyyy-mm-dd"),getDate from new Date() 我将得到 UTC 0 天,在美国,这意味着昨天,而不是今天。
  • 如果我使用另一个日期构造函数可以工作:new Date(date.getFullYear(), date.getMonth(), date.getDate()) 这给出了本地区域中的日期
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-19
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多