angularformatdatetime

formatDate angular showing incorrect minutes


I am getting timeIn in GMT+0 from the server and converting it to the clients time using

new Date(response[x].timeIn)

which gives me the correct time which for e.g is Thu Dec 10, 2020, 19:15:00 GMT+0500 (Pakistan Standard Time)

but I just want to retrieve the time from it (07:15 PM) for which I used

formatDate(new Date(response[x].timeIn), "hh:MM a", 'en-US')

but it returns 07:12 PM.

for every timeIn whether it's 10:00, 14:45, or something else, for the minute part it returns xx:12 in minutes.

Can anyone let me know if I am doing something wrong?


Solution

  • Your problem is you are setting the date format incorrectly in formatDate.

    You are using MM which is month, instead of mm, which is minutes. That's why you always get 12, because your date is always December.

    Try the following code:

    formatDate(new Date(response[x].timeIn), "hh:mm a", 'en-US')