convert a timeago date to an actual date object in typescript

To convert a timeago date string to an actual date object in TypeScript, you can use the Date object constructor and pass in the time in milliseconds.

Here's an example function that takes a timeago date string as input and returns a Date object:

index.ts
function timeagoToDate(timeago: string): Date {
  const seconds = Number(Date.parse(timeago)) / 1000;
  const milliseconds = seconds * 1000;
  const date = new Date(milliseconds);
  return date;
}
197 chars
7 lines

You can then use this function like so:

index.ts
const timeago = "2021-04-23T19:46:37.000Z";
const actualDate = timeagoToDate(timeago);
console.log(actualDate); // Fri Apr 23 2021 12:46:37 GMT-0700 (Pacific Daylight Time)
173 chars
4 lines

This will output a Date object that represents the actual date and time that the timeago string represents.

gistlibby LogSnag