G.Smith
G.Smith

Reputation: 141

Ionic / Angular date input set programatically using moment

I'm trying to set the value of a "date" input but im getting this error message :

Error: [ngModel:datefmt] Expected `03/27/2012` to be a date

I was under the impression that the correct format was MM/DD/YYYY? ive also tried DD/MM/YYYY.

I'm using moment.js to format then setting the value of the textbox.

<label class="item item-input item-stacked-label">
            <span class="input-label">Date Entered :</span>
            <input type="date" placeholder="Date Entered" value="" ng-model="entered">
          </label>

controller :

$scope.entered = moment($scope.entered).format("MM/DD/YYYY");

Upvotes: 1

Views: 553

Answers (2)

user3791775
user3791775

Reputation: 471

You could do this:

dateobj = $scope.entered

    var tempobj = {

        year: dateobj.getFullYear(),
        month: dateobj.getMonth(),
        day: dateobj.getDate()

    }

  var momentobj = moment(tempobj).format('format it the way you want to');

Upvotes: 0

Kobi Cohen
Kobi Cohen

Reputation: 678

There is a library called angular-moment, where you can use a variety of filters.

Check it out: https://github.com/urish/angular-moment

Upvotes: 1

Related Questions