Member-only story

8 Ways of Date Formatting in Javascript

habtesoft
3 min readSep 23, 2024

--

Not A Medium member? Read this article here.

JavaScript provides various ways to format dates, from using built-in methods to leveraging libraries. Let’s explore some of the most common methods:

1. Using toLocaleDateString()

The toLocaleDateString() method converts a Date object to a string, using locale conventions.

Example:

const date = new Date();

// Default locale (browser's locale)
console.log(date.toLocaleDateString()); // e.g., "9/20/2024"

// Custom locale
console.log(date.toLocaleDateString('en-GB')); // "20/09/2024" (dd/mm/yyyy)
console.log(date.toLocaleDateString('en-US')); // "09/20/2024" (mm/dd/yyyy)
console.log(date.toLocaleDateString('ja-JP')); // "2024/09/20" (yyyy/mm/dd)

// Custom options
console.log(date.toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
})); // "Friday, September 20, 2024"

2. Using Intl.DateTimeFormat

The Intl.DateTimeFormat object provides a way to format dates based on locales and options.

Example:

const date = new Date();
const formatter = new Intl.DateTimeFormat('en-GB', {
year: 'numeric',
month: 'short',
day: '2-digit',
})…

--

--

habtesoft
habtesoft

Written by habtesoft

Passionate JavaScript developer with a focus on backend technologies. Always eager to connect and learn. Let’s talk, https://buymeacoffee.com/habtesoftat

No responses yet