___Crystal
2016-10-12 22:48
我一开始不会,后来看了答案决定先显示时间然后代码是这样的
var date = new Date();实例化日期对象
var timeStr = '';
timeStr += date.getFullYear() + "年";
timeStr += date.getMonth() + 1 + "月";
timeStr += date.getDate() + "日";
var day = date.getDay();
var week = '';
switch(day){
case 0:
week='星期一';
break;
case 1:
week='星期二';
break;
case 2:
week='星期三';
break;
case 3:
week='星期四';
break;
case 4:
week='星期五';
break;
case 5:
week='星期六';
break;
case 6:
week='星期天';
break;
}
timeStr += " " + week;
document.write(timeStr);
结果什么都不显示,我用文本比对工具对照了,也是没发现问题,但是把答案的放进去就显示日期了,我觉得我的和答案对应那段没有区别啊,有哪位能帮忙看看吗?谢谢
var mydate=new Date();
var mynum=mydate.getDay();
var myarr=["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
document.write(mydate.getFullYear()+"年"+mydate.getMonth()+"月"+mydate.getDate()+"日"+" "+myarr[mynum]);
你用switch复杂了
此句话没有加 注释 ‘//'
拿到本地 看浏览器报错
var date = new Date(); //这句话注释调
var timeStr;
timeStr = date.getFullYear() + "年"; //去掉“+”
timeStr += date.getMonth() + "月";
timeStr += date.getDate() + "日";
var day = date.getDay();
var week; //这样写也行
switch(day){
case 0:
week='星期一';
break;
case 1:
week='星期二';
break;
case 2:
week='星期三';
break;
case 3:
week='星期四';
break;
case 4:
week='星期五';
break;
case 5:
week='星期六';
break;
case 6:
week='星期天';
break;
}
timeStr +=" "+week;
document.write(timeStr);
我眼花看你的代码,错误没找,不好意思哈
JavaScript进阶篇
468060 学习 · 21891 问题
相似问题