Skip to content

Instantly share code, notes, and snippets.

@Serhioromano Serhioromano/calendar.js
Last active Jan 20, 2017

Embed
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();
console.log(date.getMonthFormatted());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.