【问题标题】:Js outputs wrong day in the console for some odd reason由于某些奇怪的原因,Js 在控制台中输出错误的日期
【发布时间】:2021-08-05 15:19:22
【问题描述】:

我正在处理一个日历项目,它总是在控制台中给我错误的一天,例如,我将 14.5.21 放在我的构造函数中,这是一个星期五,而不是 5,出于某种奇怪的原因给了我 4。

在我看来,月份和年份工作得很好,只是 d.getUTCday() 输出不正确

这是我得到的输出正好错过了 1 天

 <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>

【问题讨论】:

  • 具体日期(当地时间)是 UTC 的星期五吗?
  • 不,我想我搞砸了,我希望它在我的当地时间不是通用的

标签: javascript react-fullstack


【解决方案1】:

基于 cmets:getUTCDay 将返回 UTC 日期。你想要getDay()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 2011-01-24
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 2011-02-01
    • 1970-01-01
    相关资源
    最近更新 更多