Hamp
Hamp

Reputation: 39

JS Date Object value of month is wrong

Hi guys I used Date object but value of getMonth is wrong.

$(document).on("click", "#clossing", function()
      {

        var today= new Date();
        console.log(today);
        var year=today.getFullYear();
        var month=today.getMonth();
        var date=today.getDate();
        var filename=year+"/"+month+"/"+date+".txt";
        console.log(month);

        saveToFile_Chrome(filename,"hello");

      });

Time is 2020-06-17 5:30 pm but var month is 5

Here is console.log(today):Wed Jun 17 2020 17:30:43 GMT-0400

Why is it differnt?

Upvotes: 0

Views: 40

Answers (1)

sonEtLumiere
sonEtLumiere

Reputation: 4572

Date object return a zero based month, is working fine! just add +1 to month variable.

var today= new Date();
        console.log(today);
        var year=today.getFullYear();
        var month=today.getMonth() + 1;
        var date=today.getDate();
        var filename=year+"/"+month+"/"+date+".txt";
        console.log(month);

Upvotes: 1

Related Questions