I have this string 2022-02-25 06:09 AM. How do I use return just 06:09 AM (without seconds)?
I tried two ways. One way is to convert the string into Date object and then use toLocaleTimeString. But the output is 6:09:00 AM which includes the second. Here is my code.
getMyTime(lastUpdate) {
let d = new Date(lastUpdate);
return d.toLocaleTimeString();
},
The other way is I used substring which returns 25 06:09 AM. Not sure why it get 25. Here is my code.
getMyTime(lastUpdate) {
return lastUpdate.substring(19,8);
},
Use options like this:
getMyTime(lastUpdate) {
var options = {
hour12: true, // false if you want hour without AM or PM (24 format)
hour: '2-digit',
minute: '2-digit'
};
let d = new Date(lastUpdate);
return d.toLocaleTimeString("en-GB", options);
}