孤独的小猪
2016-10-25 11:42
var mydate = new Date(); var myyear = mydate.getFullYear(); var mymonth = mydate.getMonth(); var mydate = mydate.getDate(); var weekday = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六']; var week = weekday[mydate.getDay()]; document.write(myyear+'年'+mymonth+'月'+mydate+'日'+week);
代码第4行有问题,你将变量mydate进行了重新的赋值,返回类型变成了数字,所以下面的mydate.getDay()无法正常运行
其实将第四行的变量名字改变一下,不与日期变量mydate重复就行
<script type="text/javascript">
var mydate = new Date();
var day=mydate.getDay();
var myyear=mydate.getFullYear();
var mymonth=mydate.getMonth()+1;
var mydate=mydate.getDate();
var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var myweek=weekday[day];
document.write(myyear+"年"+mymonth+"月"+mydate+"日"+myweek);
</script>
当把var day=mydate.getDay();这行代码提到var mydate = new Date();下面一行的时候,就能正常运行了。
至今不知道什么原因,应该是代码验证系统的bug吧。
<script type="text/javascript">
var mydate = new Date();
var day=mydate.getDay();
var myyear=mydate.getFullYear();
var mymonth=mydate.getMonth()+1;
var mydate=mydate.getDate();
var weekday=["星期日","星期一","星期二","星期三","星期四","星期五","星期六"];
var myweek=weekday[day];
document.write(myyear+"年"+mymonth+"月"+mydate+"日"+myweek);
</script>
['星期日','星期一','星期二','星期三','星期四','星期五','星期六']; 应该是双引号把
2个错误 : 一个是变量重复了 还有一个是月份的话要+1
var mydate = new Date();
var myyear = mydate.getFullYear();
var mymonth = mydate.getMonth()+1;
var myday = mydate.getDate();
var weekday = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
var week = weekday[mydate.getDay()];
document.write(myyear+'年'+mymonth+'月'+myday+'日'+week);
JavaScript进阶篇
468193 学习 · 21891 问题
相似问题