Just leverage the built-in toISOString method that brings your date to the ISO 8601 format:

let yourDate = new Date()
yourDate.toISOString().split('T')[0]

Where yourDate is your date object.

Edit: @exbuddha wrote this to handle time zone in the comments:

const offset = yourDate.getTimezoneOffset()
yourDate = new Date(yourDate.getTime() - (offset*60*1000))
return yourDate.toISOString().split('T')[0]
Answer from Darth Egregious on Stack Overflow
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
Independent of input format, JavaScript will (by default) output dates in full text string format: ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
The format is as follows: ... YYYY is the year, with four digits (0000 to 9999), or as an expanded year of + or - followed by six digits. The sign is required for expanded years. -000000 is explicitly disallowed as a valid year. MM is the month, with two digits (01 to 12).
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-3.php
JavaScript: Display the current date in various format - w3resource
// Get the current date var today ... (mm < 10) { mm = '0' + mm; } // Format the date as mm-dd-yyyy and log it today = mm + '-' + dd + '-' + yyyy; console.log(today); // Format the date as mm/dd/yyyy and log it today = mm + '/' ...
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › date-tostring-format-yyyy-mm-dd
Format a JavaScript Date to YYYY MM DD - Mastering JS
To format a date to YYYYMMDD in JavaScript, you can use the toLocaleDateString() function in combination with the split(), reverse(), and join() functions. The trick is that, in the UK, dates are formatted in DD/MM/YYYY format, with two digit ...
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-format-javascript-date-as-yyyy-mm-dd.php
How to Format JavaScript Date as YYYY-MM-DD
// Create a date object from a ...ng("default", { day: "2-digit" }); // Generate yyyy-mm-dd date string var formattedDate = year + "-" + month + "-" + day; console.log(formattedDate); // Prints: 2022-05-04...
Find elsewhere
🌐
Day.js
day.js.org › docs › en › display › format
Format · Day.js
dayjs().format() // current date in ISO8601, without fraction seconds e.g. '2020-04-02T08:02:17-05:00' dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]') // 'YYYYescape 2019-01-25T00:00:00-02:00Z' dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
🌐
Squash
squash.io › how-to-format-javascript-date-as-yyyy-mm-dd
How to Format JavaScript Dates as YYYY-MM-DD
We use the getFullYear() method to get the full year, getMonth() method to get the month (adding 1 because months in JavaScript are zero-based), and getDate() method to get the day.
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-date
JavaScript Date: Create, Convert, Compare Dates in JavaScript
var date1 = new Date("February 2015-3"); var date2 = new Date("February-2015-3"); var date3 = new Date("February-2015-3"); var date4 = new Date("February,2015-3"); var date5 = new Date("February,2015,3"); var date6 = new Date("February*2015,3"); var date7 = new Date("February$2015$3"); var date8 = new Date("3-2-2015"); // MM-dd-YYYY var date9 = new Date("3/2/2015"); // MM-dd-YYYY
🌐
Byby
byby.dev › js-format-date
How to parse and format a date in JavaScript
In this tutorial, we will focus on the dd/mm/yyyy format and explore various ways to parse, format, and manipulate dates in this format using JavaScript.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-format-javascript-date-as-yyyy-mm-dd
How To Format JavaScript Date as yyyy-mm-dd? - GeeksforGeeks
June 24, 2025 - We use JavaScript built-in help methods from the Date class that help us to create a formatted version of the current date in the form of yyyy-mm-dd.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript
How to Get Current Formatted Date dd/mm/yyyy in JavaScript? | GeeksforGeeks
December 4, 2024 - ... The current date in "dd/mm/yyyy" format represents the day, month, and year according to the Gregorian calendar, to obtain the current date in "dd/mm/yyyy" format using JavaScript, utilize the Date object methods to extract day, month, and ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript-and-append-it-to-an-input
How to get current formatted date dd/mm/yyyy in Javascript and append it to an input? - GeeksforGeeks
July 11, 2025 - To display dd/mm/yyyy, set the locale to 'en-GB'. Use the valueAsDate property to assign the current date as a Date object to an input field. ... Example: In this example we displays the current date using JavaScript's toLocaleDateString() method ...
🌐
Futurestud.io
futurestud.io › tutorials › how-to-format-a-date-yyyy-mm-dd-in-javascript-or-node-js
How to Format a Date YYYY-MM-DD in JavaScript or Node.js
You must ensure a leading zero as well in the formatting. In case you’re wondering about the provided getDay method in JavaScript: it returns the day of the week and not the day of the month. That’s why we’re using getDate. Here’s a sample format(date) function formatting the given date in YYYY-MM-DD:
🌐
freeCodeCamp
freecodecamp.org › news › how-to-format-a-date-with-javascript-date-formatting-in-js
How to Format a Date with JavaScript – Date Formatting in JS
November 7, 2024 - Specific Date Format: To display a date in a specific format, such as DD/MM/YYYY, you can use Intl.DateTimeFormat with the appropriate options.
Top answer
1 of 3
5

Whenever you find yourself writing code that feels the same, consider whether you can create a function to do it. For example consider using a function for padding to two digits.


Instead of turning newYear into a string so you can split it on digits, consider using the remainder operator (%). That is, var newYear = newDate.getFullYear() % 100;

Although (actually, because) javascript lets you play fast and loose with whether a variable is a string or a number or something else, it's worth avoiding switching between them where possible. It also has the slight advantage that it always gets the last two digits, even if the year had 3 or 5 digits in it. (Of course that won't be relevant for a very long time if it only prints the current date, but you shouldn't bank on that.)


I would be inclined to make this code slightly more general by allowing it to return rather than print out the formatted string, and allowing it to take a Date as a parameter rather than only using the current date. If you do want to print it, you can always do that with the output.

Of course that might actually be marginally longer, but I think that's worth it. Unless you're in a smallest number of characters competition, it is better to have readable, flexible code than short code if they conflict. Length is only a problem when there's too much there to keep in your head how it fits together.


Sticking strings together one bit at a time with + is an expensive thing to do. It's worth getting into the habit of using something like join As shown in jstudenski's code.

2 of 3
4

Your current function do a lot of unnecessary manipulations: picking month, day and year from the Date object, coverting from number to string and back, splitting, concatenating, updating on several conditions... IMHO, it's too long and unefficient.

So I'd suggest you shorten it using toLocaleDateString method of Date object prototype (see comments in the code):

console.log(
  (new Date()) // take a new Date object.
               // BTW, you can take another time just by 
               // passing any valid time string as a parameter
               // e.g. new Date('7/8/2008')

    .toLocaleDateString('en-US', {year:'2-digit', month:'2-digit', day:'2-digit'})
     // take a locale string from the Date object 
     // passing locale 'en-US' and formatting options as parameters
     // So this returns you a string like "07/08/08"
                             

)