I am getting created_at timestamp from backend in UTC format, which i need to render in browser. I am using date pipe in Angular-7. After reading few blogs and stackoverflow, i wrote this function below to get normal date format, which i can pass through date pipe in angular to format.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
- Issue: The output is in future date.
- Input: "22-12-2020 03:44:09 UTC"
- Output: "Fri Jan 22 2021 09:14:09 GMT+0530 (India Standard Time)"
getDateFromUTC(dateString: string) {
const date = dateString
.split(" ")[0]
.split("-")
.map((number) => parseInt(number));
const hours = dateString
.split(" ")[1]
.split(":")
.map((num) => parseInt(num));
const convertedDate = new Date(
Date.UTC(date[2], date[1], date[0], hours[0], hours[1],hours[2])
);
return convertedDate;
}
getDateFromUTC("22-12-2020 03:44:09 UTC")
What am i doing wrong?