How to Get the Current Date in Milliseconds Using TypeScript

While working as a Typescript developer on time-sensitive applications, I recently faced an issue where I needed to accurately capture and manipulate timestamps. It’s one of those settings that can make a big difference, especially when logging events, syncing data, or tracking real-time user actions.

In this tutorial, I will explain how to get the current date in milliseconds using TypeScript.

Understanding the Need for Milliseconds

When dealing with time-critical applications, such as logging events, measuring performance, or synchronizing data across systems, using milliseconds provides a high level of precision. Milliseconds represent the number of milliseconds that have elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC).

Using the Date.now() Method

The simplest way to get the current date in milliseconds using TypeScript is by utilizing the Date.now() method. This static method returns the number of milliseconds that have elapsed since the Unix epoch.

Here’s an example of how to use Date.now() in TypeScript:

const currentTimeInMs: number = Date.now();
console.log(currentTimeInMs); // Output: 1747033561375

Output:

Get current date in milliseconds in typescript

In this example, we declare a variable currentTimeInMs of type number and assign it the value returned by Date.now(). The output will be the current timestamp in milliseconds.

Check out: Get the Current Date in TypeScript

Real-World Example

Let’s consider a real-world scenario where a USA-based e-commerce company wants to track the time taken for a user to complete the checkout process. By using Date.now(), we can easily measure the duration:

const startTime: number = Date.now();

// Simulate the checkout process with a delay
function simulateCheckout(): Promise<void> {
  return new Promise((resolve) => {
    setTimeout(() => {
      // Simulated processing (e.g., calculating totals, saving order)
      console.log("Checkout process completed.");
      resolve();
    }, 1200); // Simulate 1200ms processing time
  });
}

simulateCheckout().then(() => {
  const endTime: number = Date.now();
  const durationInMs: number = endTime - startTime;
  console.log(`Checkout process took ${durationInMs} milliseconds.`);
});

Output:

Current date in milliseconds using TypeScript

In this example, we capture the start time using Date.now() before the checkout process begins, and after the process completes, we capture the end time. We obtain the duration in milliseconds by subtracting the start time from the end time.

Check out: How to Set a Date Value to Null in TypeScript

Using the getTime() Method

Another way to get the current date in milliseconds is by creating a new Date object and calling the getTime() method on it. The getTime() method returns the number of milliseconds since the Unix epoch for the specified date.

Here’s an example:

const currentDate: Date = new Date();
const currentTimeInMs: number = currentDate.getTime();
console.log(currentTimeInMs); // Output: 1747034901650

Output:

use getTime to fetch milliseconds date in typescript

In this code snippet, we create a new Date object representing the current date and time. By calling getTime() on this object, we retrieve the current timestamp in milliseconds.

Real-World Example

Let’s consider another real-world scenario where a USA-based social media platform wants to display the relative time since a user’s last activity. We can use getTime() to calculate the time difference:

const lastActivityDate: Date = new Date("2025-05-15T09:30:00");
const currentDate: Date = new Date();
const timeDifferenceInMs: number = currentDate.getTime() - lastActivityDate.getTime();
const timeDifferenceInMinutes: number = Math.floor(timeDifferenceInMs / 60000);
console.log(`Last activity: ${timeDifferenceInMinutes} minutes ago.`);

Output:

Get Milliseconds from Date in Typescript

In this example, we have the lastActivityDate representing the user’s last activity timestamp. We create a new Date object for the current date and time. By subtracting the getTime() values of both dates, we obtain the time difference in milliseconds. We then convert the milliseconds to minutes using simple math.

Check out: Create a Date from Year, Month, and Day in TypeScript

Using the valueOf() Method

The valueOf() method is another option to get the current date in milliseconds. It returns the primitive value of a Date object, which is the number of milliseconds since the Unix epoch.

Here’s an example:

const currentDate: Date = new Date();
const currentTimeInMs: number = currentDate.valueOf();
console.log(currentTimeInMs); // Output: 1668710400000

Output:

Typescript current date in milliseconds

Similar to the previous examples, we create a new Date object representing the current date and time. By calling valueOf() on this object, we retrieve the current timestamp in milliseconds.

Check out: How to Compare Dates Without Time in TypeScript

Real-World Example

Let’s consider a real-world scenario where a USA-based weather application needs to fetch weather data based on the current timestamp. We can use valueOf() to include the current timestamp in the API request:

const currentDate: Date = new Date();
const currentTimeInMs: number = currentDate.valueOf();
const apiUrl: string = `https://api.weather.com/data?timestamp=${currentTimeInMs}`;

console.log(`Requesting weather data using URL: ${apiUrl}`);

// Simulate an API request using fetch (or a mock for demonstration)
function mockApiRequest(url: string): Promise<string> {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(`Weather data fetched successfully for timestamp ${currentTimeInMs}`);
    }, 1000); // Simulate 1 second delay
  });
}

mockApiRequest(apiUrl).then((response) => {
  console.log(response);
});

Output:

How to get date in Milliseconds in Typescript

In this example, we create a new Date object for the current date and time. By calling valueOf(), we obtain the current timestamp in milliseconds. We then include this timestamp in the API URL to fetch weather data specific to the current time.

Conclusion

In this tutorial, we explored three different methods to get the current date in milliseconds using TypeScript: Date.now(), getTime(), and valueOf(). Each method provides a straightforward way to obtain the current timestamp, which is essential for various time-sensitive applications.

By understanding and utilizing these methods effectively, you can build robust TypeScript applications that handle time-based functionality with precision and accuracy.

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.