请大家帮我分析一下下面一段代码,谢谢!

来源:百度知道 编辑:UC知道 时间:2024/06/18 01:23:25
function clock(){
now=new date();
hours=now.getminutes();
hours=now.getseconds();
timestr=""+hours;
timestr+=((minutes<10)?":0":":")+minutes;
timestr+=((seconds<10)?":0":":")+seconds;
document.clock.time.value=timestr;

date=now.getdate();
month=now.getmonth()+1;
year=now.getyear();
……
下面就不写了

请问:1、timestr=""+hours;为什么还要加个""空字符串啊,和不加不一样吗?
2、timestr+=((minutes<10)?":0":":")+minutes;
timestr+=((seconds<10)?":0":":")+seconds;
这两句括号里怎么理解啊?
3、month=now.getmonth()+1;为什么还要加1啊?为什么其余两个不加啊?
请各位帮帮忙谢谢!

代码应该有逻辑性错误

hours=now.getminutes(); hours改为minutes
hours=now.getseconds(); hours改为seconds

timestr=""+hours 是日期型变量和""后就会转换成字符串型,所以有用

timestr+=((minutes<10)?":0":":")+minutes;
timestr+=((seconds<10)?":0":":")+seconds;
作用是把分钟和秒都加到字符串上,如果分种和秒是10以内,舍去不计

month=now.getmonth()+1 不用加1

getmonth()+1, 是因为得到的月是0-11的吧, +1 才是1-12,和现实一样,你要确认一下

timestr 在赋值的时候 加上空字符串是为了告诉脚本解释器 timestr是个字符串变量

getMonth()所返回的值为0到11的数字,比如一月份为0,二月份为1,等等 所以要加1