I am retrieving a date from an object within a loop in the following format: 2018-08-06 20:45:00
My objective is to only display "20:45" (always with two digits for minutes) in the client's timezone. To achieve this, I have implemented the below method in the methods section of vue.js:
ihaveadate(dateFromLoop) {
var d = new Date(dateFromLoop);
d.setTime(d.getTime() + new Date().getTimezoneOffset() * 60 * 1000);
var hour = d.getUTCHours();
var minutes = (d.getMinutes() < 10 ? "0" : "") + d.getMinutes();
return `${hour}:${minutes}`;
}
This method worked well except for Safari where it resulted in NaN:NaN. Is there a more elegant solution to address this issue?
Based on your feedback, I have modified the implementation as follows:
var dateFromLoop = '2018-08-06 20:45:00';
var date = Date.parse(dateFromLoop.replace(" ", "T"));
const daty = new Intl.DateTimeFormat({
hour: "numeric",
minute: "numeric"
}).format(date);
console.log(daty);