Open In App

How to Format Date in TypeScript ?

Last Updated : 01 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Formatting dates is important especially when displaying them to the users or working with date-related data. TypeScript provides various ways to achieve this.

Below are the methods to format the date data type in TypeScript:

Using toLocaleString() method

TypeScript provides the built-in toLocaleString() method that allows us to format the default date based on the user’s locale. It includes information about the date, time, and region.

Syntax

const date = new Date();
const formattedDate = date.toLocaleString();

Example: The below code uses the toLocaleString() method to format the date in TypeScript.

Javascript
const date = new Date();
const formattedDate:string = date.toLocaleString();
console.log(formattedDate);

Output:

"2/23/2024, 2:02:29 PM" 

Using toLocaleDateString() method

The toLocaleDateString method allows us to customize the date format by specifying options such as the year, month and day. We mostly use this approach when we want to control individual date components according to the desired format.

Syntax

const date = new Date();
const formattedDate = date.toLocaleDateString()

Example: The below code uses the toLocaleDateString() method to format the date in TypeScript.

Javascript
const date = new Date();
const options: Intl.DateTimeFormatOptions = {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
};
const formattedDate: string = date.toLocaleDateString(undefined, options);
console.log(formattedDate);


Output:

"February 23, 2024" 

Using Custom Formatting

We can also format the dates in TypeScript more manually using template literals. Here we can create our own custom date string literals by calling methods like getFullYear, getMonth, etc. This method provides us the full control over the format of the date.

Syntax

const date = new Date();
const dateFormat = `${date.getDay()}/${date.getMonth()}/${date.getFullYear()}`;

Example: The below code uses the custom formatting method to format the date in TypeScript.

Javascript
function dateCustomFormatting(date: Date): string {
    const padStart = (value: number): string =>
        value.toString().padStart(2, '0');
    return
    `${padStart(date.getDate())}/
            ${padStart(date.getMonth() + 1)}/
        ${date.getFullYear()} 
            ${padStart(date.getHours())}:
                ${padStart(date.getMinutes())}`;
}

const date = new Date();
const dateFormat = dateCustomFormatting(date);
console.log(dateFormat);


Output:

"23/02/2024 14:04" 

Using date-fns Library

Date-fns is a lightweight JavaScript date utility library that provides a comprehensive set of functions for manipulating and formatting dates. You can use it to achieve custom date formatting in TypeScript projects.

Installation

First, install the date-fns library using npm:

npm install date-fns

Example: The example imports the format function from the date-fns library to format the current date (currentDate) as ‘dd/MM/yyyy HH:mm’ and logs it.

JavaScript
import { format } from 'date-fns';

const currentDate: Date = new Date();
const formattedDate: string = format(currentDate, 'dd/MM/yyyy HH:mm');
console.log(formattedDate); // Output: 01/05/2024 06:10

Output:

01/05/2024 06:10


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads