I'm making an React app where I query Microsoft Graph API where I get the response date 2021-11-03T07:45:00.0000000.
When I try to convert it to a JavaScript Date like this it gives me "invalid date" on let date = new Date(dateString);
let dateString = "2021-11-03T07:45:00.0000000"
let date = new Date(dateString);
let timezoneOffset = date.getTimezoneOffset();
date.setMinutes(date.getMinutes() - timezoneOffset);
let hours = date.getHours();
let minutes = date.getMinutes();
let formattedMinutes = minutes > 9 ? minutes : '0' + minutes;
console.log(hours + ':' + formattedMinutes);
This works in normal JS but the problem is that I use a CMS (server-side JS) that doesn't support new Date(dateString) so is it possible to format the date value to 8:45 without using Date constructor in JS? (npm libraries work in the CMS so if you have any solution with third party libraries it's fine.)
A regex could help in this situation. This is a simple example when there is no timezone offset.
str = '2021-11-03T07:45:00.0000000'
const time = str.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}T([0-9:]{5})/)[1]
const timeFormatted = time.split(':').map(num => parseInt(num)).reduce((timeFormatted, num) => {
if (timeFormatted) timeFormatted += ':'
timeFormatted += num
return timeFormatted
})
console.log(timeFormatted)