get YYYYMMDD formatted date string in javascript

Since decent formatting methods for free date formats are missing in JS, we have to do it manually:

var newdate = new Date();
var datestring = newdate.getFullYear().toString() + ('00' + (newdate.getMonth() + 1)).slice(-2) + ('00' + newdate.getDate()).slice(-2);

“datestring” now contains the date in format “YYYYMMDD”, for example “20160715”.

Leave a Comment

Your email address will not be published. Required fields are marked *