0

Example

2020-05-10 18:09:00

I need to make it

date = 2020-05-10
hour = 6
minute = 9
am_pm = PM

res.data.start_date = res.data.start_at.getDate();
res.data.start_hour = res.data.start_at.getHours(); 
res.data.start_minute = res.data.start_at.getMinutes();

I tried to use javascript get method but it does not work.

Shlok Nangia
  • 2,355
  • 3
  • 16
  • 24
Nixon Teh
  • 15
  • 4
  • what is in res.data.start_at ? – Alexander May 05 '20 at 10:50
  • To use Date methods you must first have a Date, it seems you have a string. So you might parse it to a Date first, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 05 '20 at 22:47

1 Answers1

0

Seems like you are fetching the data from some API (probably your backend). You should first convert the string of the date into a Date object.

const date: Date = new Date(res.data.start_at);

Then you could use the Date object methods such as getDate(), getHours(), etc.

*Your format has a small issue with the Date object - please read Why does Date.parse give incorrect results? to implement it appropriately.

Ron Rofe
  • 738
  • 1
  • 9
  • 25
  • The OP format is not one supported by ECMA-262 and returns an invalid date in some implementations, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG May 05 '20 at 22:45