How do I retrieve the month from the current date in mm
format? (i.e. \"05\")
This is my current code:
var currentDate = new Date();
va
In order for the accepted answer to return a string consistently, it should be:
if(currentMonth < 10) {
currentMonth = '0' + currentMonth;
} else {
currentMonth = '' + currentMonth;
}
Or:
currentMonth = (currentMonth < 10 ? '0' : '') + currentMonth;
Just for funsies, here's a version without a conditional:
currentMonth = ('0' + currentMonth).slice(-2);
Edit: switched to slice
, per Gert G's answer, credit where credit is due; substr
works too, I didn't realize it accepts a negative start
argument