Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

today.getDate works on other days except on Sunday

Example of this code that works normally at a specified date, which is a Saturday.

weeks=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
  var today = new Date("September 24, 2022 01:15:00"); 
  document.getElementById("current-day").innerHTML=weeks[today.getDay()-1];
<h1 id="current-day"></h1>

As stated in my title above, this code works on other days except only on Sundays, it just says Undefined, why is that?

weeks=["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
  var today = new Date("September 25, 2022 01:15:00");
  document.getElementById("current-day").innerHTML=weeks[today.getDay()-1];
<h1 id="current-day"></h1>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your code logic looks fine but the weeks array should starts with Sunday and avoid using negative 1 with getDay method.

const weeks = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const today = new Date("September 25, 2022 01:15:00");
document.getElementById("current-day").innerHTML = weeks[today.getDay()];
<h1 id="current-day"></h1>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading