Formatting Dates in JavaScript: A Comprehensive Guide to Date Formatting

Estimated read time 3 min read

Introduction to Date Formatting in JavaScript

Working with dates is a common task in JavaScript web development. JavaScript provides a variety of options to format dates according to specific requirements, such as displaying dates in different formats, localizing them, or extracting specific components. In this article, we will explore the various techniques and methods available in JavaScript for formatting dates.

The Date Object in JavaScript

In JavaScript, the Date object is used to work with dates and times. The Date object represents a specific moment in time and provides various methods to manipulate and format dates.

Formatting Dates Using Built-in Methods

JavaScript’s Date object offers several built-in methods to format dates. Here are some commonly used methods:

toDateString()

The toDateString() method returns a string representation of the date portion of a Date object. It formats the date according to the implementation-dependent default locale settings. Here’s an example:

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

console.log(formattedDate);

toLocaleString()

The toLocaleString() method returns a string representation of the date and time portion of a Date object based on the specified locale. It allows you to format the date and time according to the conventions of a specific region or language. Here’s an example:

const date = new Date();
const options = { dateStyle: 'full', timeStyle: 'long' };
const formattedDate = date.toLocaleString('en-US', options);

console.log(formattedDate);

toLocaleDateString()

The toLocaleDateString() method returns a string representation of the date portion of a Date object based on the specified locale. It allows you to format the date according to the conventions of a specific region or language. Here’s an example:

const date = new Date();
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
const formattedDate = date.toLocaleDateString('en-US', options);

console.log(formattedDate);

Formatting Dates Using Third-Party Libraries

In addition to the built-in methods, several third-party libraries are available that offer more advanced and customizable date formatting options. Some popular date formatting libraries in JavaScript include Moment.js, Luxon, and date-fns. These libraries provide a wide range of features and flexibility for formatting, parsing, manipulating, and displaying dates.

Custom Date Formatting

JavaScript also provides methods to format dates in custom formats using pattern codes. Here are some commonly used pattern codes:

  • yyyy: Four-digit year.
  • yy: Two-digit year.
  • MMMM: Full month name.
  • MMM: Short month name.
  • MM: Two-digit month.
  • dd: Two-digit day.
  • EEEE: Full weekday name.
  • EEE: Short weekday name.
  • HH: Two-digit hour (24-hour format).
  • hh: Two-digit hour (12-hour format).
  • mm: Two-digit minute.
  • ss: Two-digit second.
  • a: AM/PM marker.

Here’s an example of custom date formatting using pattern codes:

const date = new Date();
const formattedDate = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)}`;

console.log(formattedDate);

Internationalization (Intl) API

The Internationalization (Intl

Mark Stain

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.

You May Also Like

More From Author

+ There are no comments

Add yours