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: 1747033561375Output:

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:

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: 1747034901650Output:

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:

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: 1668710400000Output:

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:

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.

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.