ingrese la descripción de la imagen aquí Obtengo resultados diferentes para console.log (monthnum); y console.log (date.getMonth ()). No entiendo por qué serían diferentes, esperaría que fueran idénticos. Incluí una imagen de lo que muestra mi consola. Como puede ver, monthnum devuelve 11 cuando estoy esperando 8.

Si bien creo que la sección relevante está al final, he incluido la totalidad de mi guión por si acaso.

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())
0
user12159191 26 sep. 2020 a las 13:33

2 respuestas

La mejor respuesta

Cuando usas solo = estás asignando el valor

Si quieres comparar usa ==

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

... entonces es lógico monthnum = 11 porque es la última asignación;)

2
john Smith 26 sep. 2020 a las 10:38

Por cierto, si desea que el mes se muestre con su nombre, puede usar este fragmento de código:

var options = { month: 'long'};
whatmonth = new Intl.DateTimeFormat('en-US', options).format(date);
0
Kiss Konrád 26 sep. 2020 a las 10:40