22

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();
var currentMonth = currentDate.getMonth() + 1;
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Michael Kniskern
  • 24,792
  • 68
  • 164
  • 231

10 Answers10

57

An alternative way:

var currentMonth=('0'+(currentDate.getMonth()+1)).slice(-2)
Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
23
if (currentMonth < 10) { currentMonth = '0' + currentMonth; }
Matt
  • 43,482
  • 6
  • 101
  • 102
  • Thanks! I originally had `if(currentMonth < 9) { currentMonth = "0" + currentMonth; }` and it did not work. Guess I need single quotes instead of double. – Michael Kniskern May 20 '10 at 22:07
  • Odd.. the type of quote shouldn't matter! Maybe an artifact of type coercion and the `+` operator .. – Matt May 20 '10 at 22:09
  • 2
    You want `< 10` else 9 wont return '09' – Alex K. May 20 '10 at 22:09
  • 1
    @Matt: a minor thing, sure, but now you have an int if it's november or december, and a string in all other cases. this could lead to some unexpected behavior upon further concatenation (if the other concatenated value is numeric, say). i'd `toString` in an `else`. – David Hedlund May 20 '10 at 22:12
  • @David Hedlund - I would agree, but it's not possible to have a leading 0 without it being a string. – Matt May 20 '10 at 23:55
  • 1
    @Matt: yes, that's why I'm suggesting that you do `toString` in the else, so that 10, 11, and 12 (can't believe i missed october in my last comment) are also strings, even though they don't have a leading 0. Gert G's solution is another way of achieving the same effect. – David Hedlund May 21 '10 at 06:23
6

One line solution:

var currentMonth = (currentDate.getMonth() < 10 ? '0' : '') + currentDate.getMonth();
3

for the date:

("0" + this.getDate()).slice(-2)

and similar for the month:

("0" + (this.getMonth() + 1)).slice(-2)
benka
  • 4,732
  • 35
  • 47
  • 58
2

ES6 version, inpired by Gert Grenander

let date = new Date();
let month = date.getMonth()+1;
month = `0${month}`.slice(-2);
Simon Arnold
  • 15,849
  • 7
  • 67
  • 85
1

If you do this

var currentDate = new Date();
var currentMonth = currentDate.getMonth() + 1;

then currentMonth is a number, which you can format as you want, see this question that will help you with formatting: How can I format an integer to a specific length in javascript?

Community
  • 1
  • 1
Igor Pavelek
  • 1,444
  • 14
  • 22
0

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

eyelidlessness
  • 62,413
  • 11
  • 90
  • 94
0
var CurrentDate = new Date();
    CurrentDate.setMonth(CurrentDate.getMonth());

    var day = CurrentDate.getDate();
    var monthIndex = CurrentDate.getMonth()+1;
    if(monthIndex<10){
        monthIndex=('0'+monthIndex);
    }
    var year = CurrentDate.getFullYear();

    alert(monthIndex);
0

An alternative with ES6 template strings

A solution for mm/yyyy. Not quite the question, but I guess remove the second part.

const MonthYear = `${dateObj.getMonth() < 10 ? '0' : '' }${dateObj.getMonth()+1}/${dateObj.getFullYear()}`

const Month = `${dateObj.getMonth() < 10 ? '0' : '' }${dateObj.getMonth()+1}`

Hillsie
  • 607
  • 1
  • 6
  • 15
0

An answer here referenced one on number formatting but without a code example. This is a general string padding problem that has been simplified since ES2017. You can use String.prototype.padStart and String.prototype.padEnd to make sure that a string is of a fixed minimum length.

The return of Date.prototype.getMonth is a number from 0 (January) to 11 (December). To be represented as strings from '01' to '12' you might do:

let date = new Date()
let month = date.getMonth() + 1
let display = month.toString().padStart(2, '0')
console.log(display) // one of "01", "02", ..., "09", "10", "11", "12"

This similarly applies to the return of Date.prototype.getDate, a number from 1 to 31:

console.log(
  new Date().getDate().toString().padStart(2, '0')
) // one of "01", "02", ..., "31"

Or simply any value that you want to be a string with a minimum length:

let greeting = 'world'.padStart(17, 'hello ')
console.log(greeting) // "hello hello world"
spkrtn
  • 1,265
  • 10
  • 18