Hi Friends đź‘‹,

Welcome to Infinitbility ❤️!

Today, we are going to learn How to convert date to milliseconds in typescript?, here we will use the date built-in method getTime() to get a date in milliseconds.

Intro to getTime()

The getTime() method returns the number of milliseconds since the ECMAScript epoch.

A number representing the milliseconds elapsed between 1 January 1970 00:00:00 UTC and the given date.

Code summary

Here, we will see what we are going to do:

  1. Get the current date and store variable
  2. Use getTime() method
  3. Print date converted milliseconds value

Code example

// Get current date and store variable
const date = new Date();

// Use `getTime()` method
const milliseconds = date.getTime();

// Print date converted milliseconds value 
console.log(milliseconds);

Code output

When you run the above code, you will get the date convert in milliseconds example output. For now, let’s check the output.

TypeScript, convert date to iso string example
TypeScript, convert date to iso string example

All the best đź‘Ť.