-1

I'm working on a calendar project that keeps giving me a wrong day in the console for instance I put 14.5.21 in my constructor which is a friday and instead of 5 it gives me 4 for some odd reason.

seems to me that the month and year work just fine only the d.getUTCday() output isn't right

this is the output I get it misses by exactly 1 day

enter image description here

 <script>
    var d=new Date(2021,4,14,0,0,0,0);
    var monthInReal=d.getMonth()+1;
    var year=d.getUTCFullYear();
    document.write("<button class='py'>Previous Year</button>");
    document.write("<h1 class='year'>",year,"</h1>");
    document.write("<button class='ny'>Next Year</button>");
    document.write("<div class='container'>");
       document.write("<table class='t1'>");
           document.write("<tr aria-colspan='7'><td><button  class='prev'>Previous Month</button></td><th class='Month'>",monthInReal,"</th><td><button class='Next'>Next Month</button></td></tr>");
           document.write("</table>");
           document.write("<table class='t2'>");
           document.write("<tr class='week'><td>Sun </td><td> Mon </td><td> Tue </td><td> Wed </td><td> Thur </td><td> Fri </td><td> Sat </td></tr>");

      document.write( "</table>");
   
   document.write( "</div>");
   document.querySelector('.prev').addEventListener('click',function() 
   {
       if (monthInReal==1){
           
           monthInReal=12;
          
           
          year-=1;
          document.querySelector('.year').innerHTML=year;
       }
       else if(monthInReal>1 )
       {
           monthInReal-=1;
          

       }  
       d.setUTCFullYear(year);
       d.setMonth(monthInReal-1);
       console.log(d.getUTCMonth());
   console.log(d.getUTCFullYear());
      
       document.querySelector('.Month').innerHTML=monthInReal;

   })
   document.querySelector('.Next').addEventListener('click',function()
   {
       if (monthInReal==12)
       {
        monthInReal=1;
          
          
         year+=1;
         document.querySelector('.year').innerHTML=year;
       }
       else if(monthInReal<12)
       {
           monthInReal++;
           
       }
       d.setMonth(monthInReal-1);
       console.log(d.getUTCMonth());
   console.log(d.getUTCFullYear());
       document.querySelector('.Month').innerHTML=monthInReal;
   })
   document.querySelector('.py').addEventListener('click',function()
   {
     year-=1;
     d.setUTCFullYear(year);
     console.log(d.getUTCMonth())
   console.log(d.getUTCFullYear())
     document.querySelector('.year').innerHTML=year;
   })
   document.querySelector('.ny').addEventListener('click',function()
   {
     year+=1;
     d.setUTCFullYear(year);
     console.log(d.getUTCMonth())
   console.log(d.getUTCFullYear())
     document.querySelector('.year').innerHTML=year;
   })
  console.log(d.getUTCDay());
</script>
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

Based on comments: getUTCDay will return the UTC day. You want getDay().

0xLogN
  • 3,289
  • 1
  • 14
  • 35