Convert date to long date format using Javascript


In this article we will look into how to convert a date to a long date format using Javascript. To convert it to a long format we will be using a toLocaleDateString() method in javascript , it let us pass arguments as options and language too.

What is toLocaleDateString() method?

The toLocaleDateString() methods converts a Date Object into a string with a language sensitive representation which is more readable for a user.

Syntax:

dateObject.toLocaleDateString()
dateObject.toLocaleDateString(locales)
dateObject.toLocaleDateString(locales, options)

The two argument : locals and options is use to customise the behaviour of the function. It let us specify the language in which we want to convert the date.

locals: This parameters is a array of locale Strings which assigns the language or locale tags. It is an optional parameter.

options: This parameters specify comparison options. Some of the properties are: timeZone, weekday, year, month, day, hour, minute, second etc

How to convert date to long date format using Javascript?

To convert a date to long format using toLocalDateString method, follow the example below:

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today  = new Date();

console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options)); // शनिवार, 17 सितंबर 2016

You can also use the toLocalDateString() methods to print out only certain parts of the date too.

For example:

date.toLocaleDateString("en-US", { day: 'numeric' }) : It will only return the day.

date.toLocaleDateString("en-US", { month: 'short' }) : It will only return the month.

date.toLocaleDateString("en-US", { year: 'numeric' }) : It will return only the year.

var today = new Date();
var day = today.toLocaleDateString("en-US", { day: 'numeric' })
var month = today.toLocaleDateString("en-US", { month:  'short' })
var year =  today.toLocaleDateString("en-US", { year: 'numeric' })

console.log(date + "-" + month + "-" + year) 

//OUTPUT
//13-Oct-2021

💾 Recommended Articles