- How to convert date to string dd/mm/yyyy format in Javascript and Node.js
- Using plain JavaScript #
- Using moment.js #
- Using date-fns #
- Using day.js #
- Date.prototype.toString()
- Try it
- Syntax
- Return value
- Description
- Examples
- Using toString()
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- Date.prototype.toISOString()
- Try it
- Syntax
- Return value
- Exceptions
- Examples
- Using toISOString()
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- How to Format Dates in JavaScript with One Line of Code
- Here’s an Interactive Scrim about Formatting Dates in JavaScript with One Line of Code
- How to Format Dates in JS
- Date Methods in JavaScript
- The toDateString() Method in JavaScript
- The toLocaleDateString() Method in JavaScript
- Conclusion
- Useful Resources
How to convert date to string dd/mm/yyyy format in Javascript and Node.js
There are many ways to convert date to string in Javascript. In this post, I will show you a few ways to format date in Javascript/Node.js to string in dd/mm/yyyy format with code example.
Using plain JavaScript #
These below examples will show you how to convert today date in Javascript to string in mm/dd/yyyy format.
var today = new Date(); var dd = String(today.getDate()).padStart(2, '0'); var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0! var yyyy = today.getFullYear(); today = mm + '/' + dd + '/' + yyyy; console.log(today);
The output will be (the date of writing this post):
Of course you can change the order of month and day to your preference. For example, you can use the below code to convert date to string in dd/mm/yyyy format.
today = dd + '/' + mm + '/' + yyyy; console.log(today);
The output will be (the date of writing this post):
If you prefer using hyphen instead of slash, you can use the below code to convert date to string in dd-mm-yyyy format.
today = dd + '-' + mm + '-' + yyyy; console.log(today);
Ouput will be (the date of writing this post):
Tip: If you just need the date in yyyy-mm-dd format, you can use the below code to convert date to string in yyyy-mm-dd format.
let today = new Date() today = today.toISOString().split('T')[0]
Output will be (the date of writing this post):
Using moment.js #
moment.js is a JavaScript library for parsing, validating, manipulating, and formatting dates and times. You can use it to convert date to string in dd/mm/yyyy format.
var today = moment().format('DD/MM/YYYY'); console.log(today);
The output will be (the date of writing this post):
It is very easy to use moment.js to convert date to string in dd/mm/yyyy format. To change your date format, just change the format string in the format() method. Here is another example to convert date to string in yyyy-mm-dd format.
var today = moment().format('YYYY-MM-DD'); console.log(today);
Using date-fns #
date-fns is modern JavaScript date utility library. date-fns provides the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js. To get started with date-fns , please visit date-fns website.
It is very easy to use date-fns to convert date to string in dd/mm/yyyy format.
import format > from 'date-fns' var day = format(new Date(2021, 11, 19), 'MM/dd/yyyy') //=> '11/19/2021' console.log(day);
For more format string, please visit date-fns format.
Using day.js #
day.js is a fast 2kB alternative to Moment.js with the same modern API. To learn more about how to install and use day.js , please visit day.js website.
You can use day.js to convert date to string in dd/mm/yyyy format.
dayjs('2021-11-19').format('DD/MM/YYYY') // '19/11/2021'
For more format string, please visit day.js format.
So those are 4 ways to format Javascript date to string in dd/mm/yyyy format or any other format you want. Use them to your advantage.
Date.prototype.toString()
The toString() method of Date instances returns a string representing this date interpreted in the local timezone.
Try it
Syntax
Return value
A string representing the given date (see description for the format). Returns «Invalid Date» if the date is invalid.
Description
The toString() method is part of the type coercion protocol. Because Date has a [@@toPrimitive]() method, that method always takes priority over toString() when a Date object is implicitly coerced to a string. However, Date.prototype[@@toPrimitive]() still calls this.toString() internally.
The Date object overrides the toString() method of Object . Date.prototype.toString() returns a string representation of the Date as interpreted in the local timezone, containing both the date and the time — it joins the string representation specified in toDateString() and toTimeString() together, adding a space in between. For example: «Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)».
Date.prototype.toString() must be called on Date instances. If the this value does not inherit from Date.prototype , a TypeError is thrown.
- If you only want to get the date part, use toDateString() .
- If you only want to get the time part, use toTimeString() .
- If you want to make the date interpreted as UTC instead of local timezone, use toUTCString() .
- If you want to format the date in a more user-friendly format (e.g. localization), use toUTCString() .
Examples
Using toString()
const d = new Date(0); console.log(d.toString()); // "Thu Jan 01 1970 00:00:00 GMT+0000 (Coordinated Universal Time)"
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 1, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
Date.prototype.toISOString()
The toISOString() method of Date instances returns a string representing this date in the date time string format, a simplified format based on ISO 8601, which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ , respectively). The timezone is always UTC, as denoted by the suffix Z .
Try it
Syntax
Return value
A string representing the given date in the date time string format according to universal time. It’s the same format as the one required to be recognized by Date.parse() .
Exceptions
Thrown if the date is invalid or if it corresponds to a year that cannot be represented in the date string format.
Examples
Using toISOString()
const d = new Date(0); console.log(d.toISOString()); // "1970-01-01T00:00:00.000Z"
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jun 1, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
How to Format Dates in JavaScript with One Line of Code
Joel Olawanle
For a long time, I’ve used libraries like Date-fns whenever I need to format dates in JavaScript. But it gets really weird whenever I do this in small projects that require simple date formats which JavaScript offers by default.
I discovered that most developers do this a lot. And I thought that this was the best way until I recently figured out that we don’t always need to use libraries to format dates in JavaScript.
In case you are curious to try this out, here is the code:👇
new Date().toLocaleDateString('en-us', < weekday:"long", year:"numeric", month:"short", day:"numeric">) // "Friday, Jul 2, 2021"
After trying this in your own code and seeing that it works, let’s understand why it works and learn some other ways of formatting dates in JavaScript with just one line of code.
Here’s an Interactive Scrim about Formatting Dates in JavaScript with One Line of Code
How to Format Dates in JS
Getting the date in JavaScript isn’t usually a problem, but formatting these dates to suit your project can be cumbersome for beginners. Because of this, most people eventually end up using libraries.
The most used method to get the date in JavaScript is the new Date() object.
By default, when you run new Date() in your terminal, it uses your browser’s time zone and displays the date as a full text string, like Fri Jul 02 2021 12:44:45 GMT+0100 (British Summer Time).
But having something like this in your web page or application is not professional and isn’t easy to read. So this forces you to look for better ways to format these dates.
Let’s take a look at some methods that operate on a date object.
Date Methods in JavaScript
There are so many methods that you can apply to the date object. You can use these methods to get information from a date object. Here are some of them:
- getFullYear() – gets the year as a four digit number (yyyy)
- getMonth() – gets the month as a number (0-11)
- getDate() – gets the day as a number (1-31)
- getHours() – gets the hour (0-23)
Unfortunately, most of these methods still needs a lot of code to convert the dates to the format we desire.
For example, new Date().getMonth() will output 6 which stands for July. For me to use July in my project, I will need to have long code like this which can really be cumbersome:
const currentMonth = new Date(); const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; console.log(months[currentMonth.getMonth()]);
Let’s take a look at two methods that you can use to format your dates in the best way so you can use them for your projects.
The toDateString() Method in JavaScript
The JavaScript toDateString() method returns the date portion of a date object in the form of a string using the following format:
- First three letters of the week day name
- First three letters of the month name
- Two digit day of the month, padded on the left a zero if necessary
- Four digit year (at least), padded on the left with zeros if necessary
new Date().toDateString(); //"Fri Jul 02 2021"
One major downside to this method is our inability to manipulate the date output the way we want it.
For example, it doesn’t give us the ability to show dates according to our language. Let’s take a look at another method which to me is still one of the best.
The toLocaleDateString() Method in JavaScript
This method returns the date object as a string using local conventions. It also takes in options as arguments which lets you/your applications customize the behavior of the function.
toLocaleDateString() toLocaleDateString(locales) toLocaleDateString(locales, options)
Let’s take a look a some examples and their outputs:
const currentDate = new Date(); const options = < weekday: 'long', year: 'numeric', month: 'short', day: 'numeric' >; console.log(currentDate.toLocaleDateString('de-DE', options)); //Freitag, 2. Juli 2021 console.log(currentDate.toLocaleDateString('ar-EG', options)) // الجمعة، ٢ يوليو ٢٠٢١ console.log(currentDate.toLocaleDateString('en-us', options)); //Friday, Jul 2, 2021
You can also decide not to use either locales or options:
new Date().toLocaleDateString() // "7/2/2021"
And you can also decide to only use locales. This will output the same information as the previous based on my browser’s time zone.
new Date().toLocaleDateString('en-US') "7/2/2021"
You can also decide to twist the options as you wish. There are 4 basic options which are:
- weekday – This outputs the day of the week depending on how you want it to appear (short or long).
- year – This outputs the year as a number
- month – This outputs the month of the year depending on how you want it to appear (short or long).
- day – Finally, this outputs the day as a number
new Date().toLocaleDateString('en-us', < weekday:"long", year:"numeric", month:"short">) // "Jul 2021 Friday" new Date().toLocaleDateString('en-us', < year:"numeric", month:"short">) // "Jul 2021"
Conclusion
The date object has about seven formatting methods. Each of these methods gives you a specific value:
- toString() gives you Fri Jul 02 2021 14:03:54 GMT+0100 (British Summer Time)
- toDateString() gives you Fri Jul 02 2021
- toLocaleString() gives you 7/2/2021, 2:05:07 PM
- toLocaleDateString() gives you 7/2/2021
- toGMTString() gives you Fri, 02 Jul 2021 13:06:02 GMT
- toUTCString() gives you Fri, 02 Jul 2021 13:06:28 GMT
- toISOString() gives you 2021-07-02T13:06:53.422Z
If you are looking for more advanced date formats, then you will need to create a custom format yourself. Check out the resources below to help you understand how to create custom date formats.