Javascript 由于一些奇怪的原因,Js在控制台中输出了错误的日期
我正在做一个日历项目,它总是在控制台中给我一个错误的日期,例如,我把14.5.21放在我的构造函数中,这是一个星期五,而不是5,因为一些奇怪的原因,它给了我4 在我看来,月份和年份工作正常,只有d.getUTCday()输出不正确 这是我得到的输出,它正好错过了1天Javascript 由于一些奇怪的原因,Js在控制台中输出了错误的日期,javascript,react-fullstack,Javascript,React Fullstack,我正在做一个日历项目,它总是在控制台中给我一个错误的日期,例如,我把14.5.21放在我的构造函数中,这是一个星期五,而不是5,因为一些奇怪的原因,它给了我4 在我看来,月份和年份工作正常,只有d.getUTCday()输出不正确 这是我得到的输出,它正好错过了1天 var d=新日期(2021,4,14,0,0,0); var monthInReal=d.getMonth()+1; var year=d.getUTCFullYear(); 文件。填写(“上一年度”); 文件。填写(“,年,
var d=新日期(2021,4,14,0,0,0);
var monthInReal=d.getMonth()+1;
var year=d.getUTCFullYear();
文件。填写(“上一年度”);
文件。填写(“,年,”);
文件。填写(“下一年”);
文件。填写(“”);
文件。填写(“”);
文件。填写(“上个月”,上个月,下个月”);
文件。填写(“”);
文件。填写(“”);
文件。书写(“星期一、星期二、星期三、星期四、星期五、星期六”);
文件。填写(“”);
文件。填写(“”);
document.querySelector('.prev').addEventListener('click',function())
{
如果(monthInReal==1){
蒙提尼亚尔=12;
年份-=1;
document.querySelector('.year')。innerHTML=year;
}
否则如果(蒙蒂尼雷亚尔>1)
{
monthInReal-=1;
}
d、 setUTCFullYear(年);
d、 设定月(月-1);
log(d.getUTCMonth());
log(d.getUTCFullYear());
document.querySelector(“.Month”).innerHTML=monthInReal;
})
document.querySelector('.Next').addEventListener('click',function()
{
如果(monthInReal==12)
{
monthInReal=1;
年份+=1;
document.querySelector('.year')。innerHTML=year;
}
否则,如果(monthInReal根据注释:getUTCDay
将返回UTC日期。您希望getDay()
该特定日期(在当地时间)是UTC的周五吗?不,我想我搞错了,我希望它在当地时间而不是世界时间
<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>