I receive a date from an object in a loop in this format : 2018-08-06 20:45:00
And I want to display only "20:45" (always with two digits for minutes) in the Timezone of the client. I have created this method in the methods part 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}`;
}
It seemed to work fine except that on safari I have NaN:NaN, is there a clean solution to this problem ?
After reading ur answers i made it works like that :
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);