var year = date.getFullYear();var month = date.getMonth()+1;var day = date.getDate();var h = date.getHours();var m = date.getMinutes();var s = date.getSeconds();
接下来把它们连接在一起:
var all =("当前时间是:"+ year +'年'+ month +'月'+ day +'日'+ h +':'+ m +':'+ s);
然后用代码来使其在html页面显示:
document.write(all);
然而想要时间一直走动就要使用函数:
functionshowTime(){var date=newDate();var year = date.getFullYear();var month = date.getMonth()+1;var day = date.getDate();var h = date.getHours();var m = date.getMinutes();var s = date.getSeconds();
h=h<10?'0'+h:h;
m=m<10?'0'+m:m;
s=s<10?'0'+s:s;
all =("当前时间是:"+ year +'年'+ month +'月'+ day +'日'+ h +':'+ m +':'+ s);
document.write(all);}setInterval(showTime,0);
用代码块来实现时间走动:首先要创造日期对象:var date=new Date();接下来提取时间里的年,月,日,时,分,秒: var year = date.getFullYear(); var month = date.getMonth() + 1; var day = date.getDate(); ...