AngularJS材质Datepicker更改格式

时间:2018-05-17 19:20:04

标签: javascript angularjs datepicker angular-material

我正在为项目使用AngularJS Datepicker。在发布日期时,我成功获取数据,但问题是我的日期格式为2018-05-23T06:00:00.000Z。

有没有办法让它只显示2018-05-23?

这就是我当前的datepicker

  // Date picker


  $scope.myDate = new Date();

  $scope.minDate = new Date(
    $scope.myDate.getFullYear(),
    $scope.myDate.getMonth() - 2,
    $scope.myDate.getDate());

  $scope.maxDate = new Date(
    $scope.myDate.getFullYear(),
    $scope.myDate.getMonth() + 2,
    $scope.myDate.getDate());

  $scope.onlyWeekdaysPredicate = function (date) {
  var day = date.getDay();
  return day === 1 || day === 2 || day === 3 || day === 4 || day === 5;
  };

这是我的HTML

<md-input-container style="margin:0px;position:relative;right:15px;font-size:15px">
   <md-datepicker ng-model="user.next_class" ng-model="myDate" md-placeholder="Pick a date" md-date-filter="onlyWeekdaysPredicate" date:"MM/dd/yyyy 'at' h:mma" md-open-on-focus></md-datepicker>
</md-input-container>

我听说过为此使用Moment.JS,目前正在尝试寻找可能有帮助的资源。

2 个答案:

答案 0 :(得分:1)

您可以将日期转换为toISOString(),然后将日期部分切片

var date1 = "2018-05-23T06:00:00.000Z";

var d = new Date(date1).toISOString().slice(0, 10);

console.log(d)

答案 1 :(得分:0)

对, momentJs提供了广泛的格式化选项,您可以使用各种功能,方便使用momentJS库。请检查以下格式以转换为您需要的任何dateFormat。

`var myDate = 2018-05-23T06:00:00.000Z; //这是你的datepicker ng-model变量 var customizedDate = moment(myDate).format(&#34; YYYY-MM-DD&#34;);

相关问题