Menu

How format a date in JavaScript to YY-MM-DD

23 March 2020

Dates are a recurring theme in most web applications. Whether your editing a form, sending data to an endpoint or trying to create the right timestamp, formatting your dates correctly is incredibly important. So, how do you change the format of a date to YY-MM-DD?

This can be achieved natively or with moment.js. To achieve this using standard JavaScript try the following:

Essentially, using JavaScript’s various native date methods, we can construct an array where each index has the correct date format to yy-mm-dd. Then we can utilize .join('-')to combine each element of the array into a string with each element separated by a dash.

I like this method a lot as there isn’t too much bloat. Alternatively, moment.js is one of, if not the most popular date manipulation libraries for JavaScript.