Convert date to long date format using JavaScript

featured Image

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 : localsand 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

Related Topics:

Get Tomorrow’s Date using JavaScript – Quick Way

Code on how to get Yesterday date in JavaScript

Convert A Unix Timestamp To Time In JavaScript

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more
featured Image

How to convert an array to object in javascript?

Here, in this article, we will learn how to convert an array to an object using JavaScript. Converting between arrays and objects is a common task in JavaScript. There are…

Read more