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
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?
for the date:
("0" + this.getDate()).slice(-2)
and similar for the month:
("0" + (this.getMonth() + 1)).slice(-2)
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}`