J. Perk
J. Perk

Reputation: 45

AngularJS Material Datepicker change format

I am using the AngularJS Datepicker for a project. When posting a date I succeed on getting data back but the problem is that I get the date in the format of 2018-05-23T06:00:00.000Z.

Is there a way to get it to show just 2018-05-23?

This is what I have as my current 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;
  };

This is my 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>

I have heard of using Moment.JS for this and currently trying to find a resource that may help.

Upvotes: 0

Views: 662

Answers (2)

Kishore Konangi
Kishore Konangi

Reputation: 554

Yeah, momentJs provides wide range of Formatting Options and You can have various of functions which are handy with momentJS library. Please check the below format for converting to any dateFormat you needed.

`var myDate = 2018-05-23T06:00:00.000Z ; // This is your datepicker ng-model variable var customizedDate = moment(myDate).format("YYYY-MM-DD");

Upvotes: 0

Muhammad Usman
Muhammad Usman

Reputation: 10148

You can convert your date to toISOString() and then slice the date part out of it

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

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

console.log(d)

Upvotes: 1

Related Questions