Convert date to long date format using Javascript
Learn 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.
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
Related Posts
Get Tomorrow's Date using JavaScript - Quick Way
Tutorial on how to get tomorrow's date using JavaScript and momentjs library
Code on how to get Yesterday date in JavaScript
This article is about how to get yesterday's date in JavaScript. We will get current date using new Date() and the substract one day from it.
Fix error:0308010C:digital envelope routines::unsupported
Learn to solve the "Error: error:0308010C:digital envelope routines::unsupported" error in Node.js application (Vue, Angular or react) using the solutions in this article.
Press Shift + Enter for new Line in Textarea
Override default textarea behavior on Enter key press without Shift, prevent new lines, and take custom actions like submitting forms instead. Still allow Shift+Enter newlines.
