1

I would like to get the month and date of the actual date of today. So far I have this code:

var d = new Date();   

var month = d.getMonth() + 1;
var year = d.getFullYear();
var day = d.getUTCDate();

var highestDate = new Date(d.getFullYear(), d.getMonth() + 1, 0).getDate();

var fromDateString = year + "-" + month + "-01";
var toDateString = year + "-" + month + "-" + day;

console.log(fromDateString + " - " + toDateString)

The only problem is that getUTCDate()/getDate() is not returning 01-31 but rather 1-31. That is causing the problem that I have only one number between 1-9 and not two like 01, 02, 03, 04, 05 and so on..

Same goes for the month. I only get 1-12 instead of 01-12 with getMonth() + 1. How can I get the current date and month in two digits instead of only one?

I already tried this but it isn't working because I have not Date format at the end:

if(month == 1){
    month = "0" + month;
}

I would appreciate any kind of help! Thank you!

Don
  • 89
  • 1
  • 9

0 Answers0