user12159191
user12159191

Reputation:

When using date.getMonth(), why is my result off by 3?

enter image description hereI am getting different results for console.log(monthnum); and console.log(date.getMonth()). I don't understand why they would be different, I would expect them to be identical. I've included an image of what my console shows. As you can see, monthnum returns 11 when I am expecting 8.

While I think the relevant section is at the very end I've included the entirety of my script in case.

const HOURHAND = document.querySelector("#hourhand");

const MINUTEHAND = document.querySelector("#minutehand");

const SECONDHAND = document.querySelector("#secondhand");




var date = new Date();

var whathour = date.getHours();

var whatminute = date.getMinutes();

var whatsecond = date.getSeconds();



var secdeg = whatsecond*6;

var mindeg = whatminute*6+whatsecond/10;

var hourdeg = whathour*30+whatminute/2+whatsecond/120;




let positionhour = hourdeg;
let positionminute =mindeg;
let positionsecond =secdeg;





HOURHAND.style.cssText = "transform: rotate("+positionhour + "deg)";

MINUTEHAND.style.cssText = "transform: rotate("+positionminute+"deg)";

SECONDHAND.style.cssText = "transform: rotate("+positionsecond +"deg)";

let adjhour;
if (whathour>12 && whatminute > 9) {adjhour = whathour-12; document.querySelector("#digtimehere").innerHTML = "The time is currently " + adjhour +":" + whatminute + " PM." ;}
else if (whathour>12 && whatminute <10) {adjhour = whathour-12; document.querySelector("#digtimehere").innerHTML = "The time is currently " + adjhour +":" + "0" + whatminute + " PM." ;}
else if (whatminute > 9) {adjhour = whathour; document.querySelector("#digtimehere").innerHTML = "The time is currently " + adjhour +":" + whatminute + " AM." ;}
else {adjhour = whathour; document.querySelector("#digtimehere").innerHTML = "The time is currently " + adjhour +":" + "0" + whatminute + " AM." ;}



var daynum = date.getDay();
if (daynum = 0) {whatday = "Sunday";}
if (daynum = 1) {whatday = "Monday";}
if (daynum = 2) {whatday = "Tuesday";}
if (daynum = 3) {whatday = "Wednesday";}
if (daynum = 4) {whatday = "Thursday";}
if (daynum = 5) {whatday = "Friday";}
if (daynum = 6)  {whatday = "Saturday";}

var monthnum = date.getMonth();
if (monthnum = 0) {whatmonth = "January";}
if (monthnum = 1) {whatmonth = "February";}
if (monthnum = 2) {whatmonth = "March";}
if (monthnum = 3) {whatmonth = "April";}
if (monthnum = 4) {whatmonth = "May";}
if (monthnum = 5) {whatmonth = "June";}
if (monthnum = 6) {whatmonth = "July";}
if (monthnum = 7) {whatmonth = "August";}
if (monthnum = 8) {whatmonth = "September";}
if (monthnum = 9) {whatmonth = "October";} 
if (monthnum = 10) {whatmonth = "November";}
if (monthnum = 11) {whatmonth = "December";}

var thedate = date.getDate();

document.querySelector("#datehere").innerHTML = "Today is " + whatday + ", " + whatmonth + " " + thedate + ".";
console.log(monthnum);
console.log(date);
console.log(date.getMonth())

Upvotes: 0

Views: 71

Answers (2)

Kiss Konr&#225;d
Kiss Konr&#225;d

Reputation: 68

By the way, if you want the month to be displayed using the name of it, you can use this bit of code:

var options = { month: 'long'};
whatmonth = new Intl.DateTimeFormat('en-US', options).format(date);

Upvotes: 0

john Smith
john Smith

Reputation: 17936

when using single = you are assigning the value

if you want to compare use ==

if( monthnum == 8){...}

... so its logical monthnum = 11 because it´s the last assignment ;)

Upvotes: 2

Related Questions