0

I know this type of question is not really apropiated, but I'm stuck on it and some help would be apreciated it. I have the following date:

2018-04-26T08:19:30+02:00

But when I try to get the Hours and Minutes, the result I get is:

20:19 p

What I'm doing is the following:

let momentDay = (moment(el.time,'HH:mm a').format('HH:mm a'));

I don't know how I could get the real time, the one should give me: 08:19 instead of 20:19

Thanks

Joan
  • 233
  • 1
  • 5
  • 17
  • 1
    Possible duplicate of [How to get am pm from the date time string using moment js](https://stackoverflow.com/questions/44971954/how-to-get-am-pm-from-the-date-time-string-using-moment-js) – Tan Duong May 27 '18 at 08:39
  • The second argument to the constructor/parser is the format of the entire string you're passing, not the output format you want. – RobG May 27 '18 at 10:54

1 Answers1

0

You should use hh for twelve hour time, rather than HH, which is for 24 hour time.

Also, if el.time is a datetime object, or a recognizable unambiguous date format (as yours is), then you don't need to specify a format in the constructor:

let momentDay = moment(el.time).format('hh:mm aa');

paul
  • 21,653
  • 1
  • 53
  • 54
  • Re: "*…a recognizable unambiguous date format (as yours is), then you don't need to specify a format in the constructor*". Within the [moment.js documentation](https://momentjs.com/docs/#/parsing/) is "*For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.*" So for any format other than YYYY-MM-DDTHH:mm:ss.SSSZ, the format should be supplied. There was an attempt to have moment.js not parse other formats without the format being specified, but that seems to have been discontinued. – RobG May 27 '18 at 10:52
  • 1
    `2018-05-26T23:59:57.568`, `2018-05-26T23:59` and `2018-146` are all ISO8601 strings, it is not always necessary to provide precision down to the millisecond. There's a case for including a format when not necessary (self-documenting your code), but it's not essential in all cases. – paul May 27 '18 at 11:28