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?
var d = new Date(date); date = [d.getFullYear(), ('0' + (d.getMonth() + 1)).slice(-2), ('0' + d.getDate()).slice(-2) ].join('-');
.join('-')to combine each element of the array into a string with each element separated by a dash.
It makes formatting very straightforward:
var todaysDate = new Date(); var dateString = moment(todaysDate).format("YYYY-MM-DD"); var dateStringWithTime = moment(todaysDate).format("YYYY-MM-DD HH");