Convert Number to Date in TypeScript

When I was working on a TypeScript project recently, I got a requirement to show dates from a system that sent me big numbers like 1623456789000. These numbers are called timestamps, and they represent a date and time, but in a format that’s hard to read.

In this blog, I will show you how to convert number to date in TypeScript by taking those numbers and turning them into normal dates. You will also learn how to display the dates in a way that is easy to understand.

Understanding Unix Timestamps and Milliseconds

Before diving into the conversion process, let’s clarify what Unix timestamps and milliseconds represent:

  • Unix Timestamp: A Unix timestamp is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT). It is commonly used to represent dates and times in programming.
  • Milliseconds: Milliseconds represent the number of milliseconds that have passed since January 1, 1970 (midnight UTC/GMT). They provide a more precise measurement of time compared to Unix timestamps.

Converting Unix Timestamp to Date in TypeScript

Let’s say you have a Unix timestamp, such as 1723456789, and you want to convert it to a readable date format. Here’s how you can achieve that in TypeScript:

const unixTimestamp = 1744711200000;
const date = new Date(unixTimestamp * 1000);
console.log(date.toLocaleString('en-US')); // Output: 4/15/2025, 5:43:09 PM

Output:

Number to Date conversion in TypeScript

In this example, we multiply the Unix timestamp by 1000 because the Date constructor expects the value in milliseconds. By default, toLocaleString() method returns the date and time in the format specific to the United States.

Check out: How to Compare Dates Without Time in TypeScript

Converting Milliseconds to Date in TypeScript

If you have a value in milliseconds, you can directly pass it to the Date constructor to create a date object. Here’s an example:

const milliseconds = 1744718400000;
const date = new Date(milliseconds);
console.log(date.toLocaleString('en-US', { timeZone: 'America/New_York' })); // Output: 4/15/2025, 8:00:00 AM

Output:

Convert Number to Date in TypeScript

In this case, we specify the timeZone option to ensure the date is displayed in the Eastern Time Zone (ET), commonly used in the United States.

Check out: How to Get the Current Date in Milliseconds Using TypeScript

Formatting the Date

Once you have converted the number to a date object, you can format it according to your requirements. TypeScript provides several methods to customize the date format:

  1. toLocaleDateString(): Returns the date portion of the Date object as a string, using locale conventions.
const date = new Date(1744711200000);
console.log(date.toLocaleDateString('en-US')); // Output: 4/15/2025

Output:

Convert Number to Date in TypeScript with Unixtime stamp
  1. toLocaleTimeString(): Returns the time portion of the Date object as a string, using locale conventions.
const date = new Date(1744456789000);
console.log(date.toLocaleTimeString('en-US')); // Output: 4:49:49 PM

Output:

Typescript Number to date conversion

Check out: How to Format Dates in TypeScript

  1. Custom Formatting: You can use the toLocaleString() method with options to customize the date and time format.
const date = new Date(1751189589000);

const options: Intl.DateTimeFormatOptions = {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  timeZone: 'America/New_York',
};

console.log(date.toLocaleString('en-US', options));
// Output: Sunday, June 29, 2025, 5:33:09 AM

Output:

How to convert number to date in Typescript

Handling Invalid Dates

When converting a number to a date, it’s important to handle cases where the input may be invalid or outside the valid range of dates. You can use the isNaN() function to check if the resulting date is valid.

const timestamp = 'invalid';
const date = new Date(Number(timestamp));

if (isNaN(date.getTime())) {
  console.log('Invalid date');
} else {
  console.log(date.toLocaleString('en-US'));
}

In this example, if the timestamp is not a valid number, the date will be an invalid date, and isNaN(date.getTime()) will return true.

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

Real-World Example

Let’s consider a real-world scenario where you need to convert a Unix timestamp to a formatted date in a TypeScript application. Suppose you are building a weather app that retrieves weather data from an API, and the API response includes a Unix timestamp for the forecast date.

interface WeatherData {
  location: string;
  timestamp: number;
  temperature: number;
  humidity: number;
}

const weatherData: WeatherData = {
  location: 'New York',
  timestamp: 1743456789,
  temperature: 25.5,
  humidity: 60,
};

const forecastDate = new Date(weatherData.timestamp * 1000);
const formattedDate = forecastDate.toLocaleString('en-US', {
  weekday: 'long',
  year: 'numeric',
  month: 'long',
  day: 'numeric',
});

console.log(`Weather forecast for ${weatherData.location} on ${formattedDate}:`);
console.log(`Temperature: ${weatherData.temperature}°C`);
console.log(`Humidity: ${weatherData.humidity}%`);

Output:

Number to Date data conversion in Typescript
Weather forecast for New York on Tuesday, April 1, 2025:
Temperature: 25.5°C
Humidity: 60%

In this example, we define an WeatherData interface to represent the structure of the weather data received from the API. We convert the Unix timestamp (weatherData.timestamp) to a date object using new Date(weatherData.timestamp * 1000) and then format it using toLocaleString() with specific options to display the date in a readable format.

Check out: ISO Date Strings in TypeScript

Conclusion

Converting a number to a date in TypeScript is a common task when working with timestamps or milliseconds. By using the Date constructor and various formatting methods like toLocaleString(), toLocaleDateString(), and toLocaleTimeString(), you can easily convert numbers to dates and customize their format according to your needs.

With these techniques, you can confidently work with dates in your TypeScript projects, whether you’re building web applications, server-side tools, or data-processing scripts.

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.