Epoch and Date Time Conversion in TypeScript

TypeScript is a is an open-source programming langauge. It builds on JavaScript with the goal for the development of large applications and transcompiles to JavaScript. In TypeScript, the Date() object represents a date and time functionality.

Here we will explain TypeScript date time functions to get current epoch or Unix timestamp, convert timestamp to date and convert date to epoch or Unix timestamp.

Get current epoch or Unix timestamp in TypeScript

You can get the current timestamp using Date() object and getTime() in TypeScript like below:

new Date().getTime()


Convert epoch or Unix timestamp to date in TypeScript

You can also convert unix timestamp to date using Date() object in TypeScript like below:

let unixTimestamp = 1624705077;
let formattedDate = new Date(unixTimestamp * 1000);


Convert date to epoch or unix timestamp in TypeScript

You can convert date to unix timestamp using Date() object and getTime() function TypeScript.

(new Date("2021-07-04")).getTime();


More about date time in TypeScript