0

I'm trying to convert 12 hour string to 24 Date object

day.from = day.from || moment("6:00", ["h:mm"]).format("HH:mm");

but I'm getting this error:

angular.js:11706 Error: [ngModel:datefmt] Expected `6:00` to be a date

any idea please?

VincenzoC
  • 30,117
  • 12
  • 90
  • 112
ragnar
  • 1
  • 1
  • 3
  • What is day.from ? also you should check these https://docs.angularjs.org/error/ngModel/datefmt?p0=2015-05-29T19:06:16.693209Z http://stackoverflow.com/questions/30537886/error-ngmodeldatefmt-expected-2015-05-29t190616-693209z-to-be-a-date-a – Ujjwal kaushik Sep 24 '16 at 16:49
  • `moment` is not a `date` object. angular knows nothing about moment but moment has methods to return date – charlietfl Sep 24 '16 at 19:01

2 Answers2

0

Here is a working fiddle, not throwing any error for me

 moment("6:00", ["h:mm"]).format("HH:mm") // working fine
AbhiGoel
  • 201
  • 1
  • 6
0

The error message makes me think that your date.from should be a Date object instead of a String. Note that format returns a String. To get a Date object from moment you can use toDate(), so you will have the following code:

day.from = day.from || moment("6:00", "h:mm").toDate();

PS. You don't need an array of formats when parsing your String since you have only one element inside it, that's why I used "h:mm" instead of ["h:mm"].

VincenzoC
  • 30,117
  • 12
  • 90
  • 112