How can I parse dates coming in this example format 7 July 2021 at 1:36:23 AM Z
in pure javascript? Tried a lot of things even with moment.js but was not fruitful. I presume the at
in the middle of the date is the problem. Any help would be appreciated. Thanks in advance
Edit :
Tried the replace()
function as mentioned in the comments, It worked perfectly for chrome, but safari is throwing Invalid Date
error.
Sample code:
function convertUTCDateToLocalDate(date) {
let newDate = new Date(date);
let options = {
year: "numeric",
month: "long",
day: "2-digit",
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
};
return newDate.toLocaleDateString("en", options);
}
let dateString = "7 July 2021 at 1:36:23 AM Z"
console.log(convertUTCDateToLocalDate(dateString.replace('at', ''));