Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get month in mm format in javascript

Tags:

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; 
like image 452
Michael Kniskern Avatar asked May 20 '10 22:05

Michael Kniskern


People also ask

How do I get month and date of JavaScript in 2 digit format?

Use the getMonth() method to get the month for the given date. Use the getDate() method to get the day of the month for the given date. Use the padStart() method to get the values in a 2-digit format.

How can I get current month?

Use the new Date() constructor to get a date object. Call the getMonth() method on the object and add 1 to the result. The getMonth method returns a zero-based month index so adding 1 returns the current month.

What does getMonth return?

The getMonth() method returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).

What value is returned by getMonth in April in JavaScript?

Javascript date getMonth() method returns the month in the specified date according to local time. The value returned by getMonth() is an integer between 0 and 11.


1 Answers

An alternative way:

var currentMonth=('0'+(currentDate.getMonth()+1)).slice(-2) 
like image 106
Gert Grenander Avatar answered Sep 28 '22 02:09

Gert Grenander