How can I properly format a date in JavaScript

0 votes
With the help of a proper code explanation, can you tell me How I can properly format a date in JavaScript?
Jan 9, 2025 in Java-Script by Ashutosh
• 33,370 points
• 1,027 views

1 answer to this question.

0 votes

You can format a date in JavaScript using toLocaleDateString() Method:

The toLocaleDateString() method returns a string representing the date portion of a Date object, according to locale-specific conventions. It allows for customization through options.

Example:

const date = new Date(2025, 0, 2); // January 2, 2025

// Default locale

console.log(date.toLocaleDateString()); // e.g., "1/2/2025" in en-US

// Specified locale with options

console.log(date.toLocaleDateString('en-GB', {

  weekday: 'long', // "Thursday"

  year: 'numeric', // "2025"

  month: 'long',   // "January"

  day: 'numeric'   // "2"

})); // Output: "Thursday, 2 January 2025"

answered Jan 10, 2025 by anonymous

Related Questions In Java-Script

0 votes
0 answers

How do I format a date in JavaScript?

May 7, 2022 in Java-Script by narikkadan
• 86,360 points
• 1,371 views
0 votes
0 answers

How can I create a two dimensional array in JavaScript?

Is it possible to create a two-dimensional ...READ MORE

May 9, 2022 in Java-Script by Kichu
• 19,040 points
• 1,615 views
0 votes
1 answer

How can I replace every instance of a string in JavaScript?

In JavaScript, replacing all instances of a ...READ MORE

answered Jan 10, 2025 in Java-Script by Navya
• 1,201 views
0 votes
1 answer
0 votes
1 answer

How can I configure lazy loading for Angular modules?

To configure lazy loading in Angular, you ...READ MORE

answered Dec 12, 2024 in Angular by Navya
• 1,327 views
0 votes
1 answer
0 votes
1 answer

How do I create a custom slider in React?

Create a custom slider in React by ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
• 1,129 views
0 votes
1 answer

How to generate optimized source with and without sourcemap for production use?

To generate optimized source code with or ...READ MORE

answered Dec 31, 2024 in PHP by Navya
• 2,573 views
0 votes
1 answer

How can I iterate through the properties of a JavaScript object?

You can iterate through the properties of ...READ MORE

answered Jan 10, 2025 in Java-Script by Navya
• 1,036 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP