Skip to content

Get month name and weekday from a JavaScript date

In this quick post, you will learn how to extract month name, week day from a JavaScript date. JavaScript Date object’s toLocaleString() provides various properties with which you can extract information related to a date in any format.

Syntax

date.toLocaleString(locale, {propertiesJson})

Let’s define a date from which we are going to extract month name and weekday from a JavaScript date.

let today = new Date();

Month

let month = today.toLocaleString('default', { month: 'long'});

console.log(month);

//April

Weekday

let weekday = today.toLocaleString('default', { weekday:'long'});
console.log(weekday);

//Saturday

Day

let day = today.toLocaleString('default', {day:'numeric'});
console.log(day);

//11

Year

let year = today.toLocaleString('default', {year:'numeric'});
console.log(year);

//2020

Combining all these, you can get the current date in a beautiful format.

let sentDate = today.toLocaleString('default', { weekday:'long', month: 'long', day:'numeric',year: 'numeric' });
console.log("sentDate --" + sentDate);


//sentDate --Saturday, 11 April 2020
See also  How to return multiple values from a JavaScript function?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.