Hi Friends đź‘‹,

Welcome to Infinitbility ❤️!

Today, we are going to learn How to convert date to epoch time in typescript, here we will use built-in method Date.parse() method to convert date to epoch time.

First of all, let understand what is a epoch time?

The Unix epoch or Unix time is the number of seconds elapsed since January 1, 1970. The getTime() method is used to return the millisecond representation of the Date object.31

So First we will create a date variable, and then we will use Date.parse() method to convert date to epoch time.

let’s dive in code…

let date = new Date();
console.log(date); // 2022-04-26T08:49:11.282Z

let numDate: number = Date.parse(date);
console.log(numDate) // 1650962945000

Well, when you run above code, you can able to see date converted in epoch time format.

For now, let’s check the output.

Output

TypeScript, convert date to epoch time example
TypeScript, convert date to epoch time example

All the best đź‘Ť.