Skip to content

Instantly share code, notes, and snippets.



Last active Mar 4, 2021
What would you like to do?
How to format date in Javascript into 2 digits format. By default javascript `getMonth()` return single digit and even start months index with 0. The best way around is to prototype Javascript core Date class so later you can use it anywhere within your script. The same apply to `getDate()`
Date.prototype.getMonthFormatted = function() {
var month = this.getMonth() + 1;
return month < 10 ? '0' + month : month;
Date.prototype.getDateFormatted = function() {
var date = this.getDate();
return date < 10 ? '0' + date : date;
var date = new Date();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment