完整显示当前日期和时间的JS代码
程序员文章站
2023-11-06 20:22:40
代码演示效果为“2007年2月25日星期日正午12:42:48”。 使用方法:将下面的js代码放到你想要显示的页面中(支持html页面),然后在你想要显示时间的位置插入...
代码演示效果为“2007年2月25日星期日正午12:42:48”。
使用方法:将下面的js代码放到你想要显示的页面中(支持html页面),然后在你想要显示时间的位置插入下面的代码即可
<div id="clock" align="center" style="font-size: 12px; color:#000000"></div>
js代码如下:
<script>
function tick() {
var hours, minutes, seconds, xfile;
var inthours, intminutes, intseconds;
var today, theday;
today = new date();
function initarray(){
this.length=initarray.arguments.length
for(var i=0;i<this.length;i++)
this[i+1]=initarray.arguments[i] }
var d=new initarray(
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六");
theday = today.getyear()+"年" + [today.getmonth()+1]+"月" +today.getdate() + d[today.getday()+1];
inthours = today.gethours();
intminutes = today.getminutes();
intseconds = today.getseconds();
if (inthours == 0) {
hours = "12:";
xfile = "午夜";
} else if (inthours < 12) {
hours = inthours+":";
xfile = "上午";
} else if (inthours == 12) {
hours = "12:";
xfile = "正午";
} else {
inthours = inthours - 12
hours = inthours + ":";
xfile = "下午";
}
if (intminutes < 10) {
minutes = "0"+intminutes+":";
} else {
minutes = intminutes+":";
}
if (intseconds < 10) {
seconds = "0"+intseconds+" ";
} else {
seconds = intseconds+" ";
}
timestring = theday+xfile+hours+minutes+seconds;
clock.innerhtml = timestring;
window.settimeout("tick();", 100);
}
window.onload = tick;
</script>
比较实用的代码,值得珍藏^_^
使用方法:将下面的js代码放到你想要显示的页面中(支持html页面),然后在你想要显示时间的位置插入下面的代码即可
<div id="clock" align="center" style="font-size: 12px; color:#000000"></div>
js代码如下:
<script>
function tick() {
var hours, minutes, seconds, xfile;
var inthours, intminutes, intseconds;
var today, theday;
today = new date();
function initarray(){
this.length=initarray.arguments.length
for(var i=0;i<this.length;i++)
this[i+1]=initarray.arguments[i] }
var d=new initarray(
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六");
theday = today.getyear()+"年" + [today.getmonth()+1]+"月" +today.getdate() + d[today.getday()+1];
inthours = today.gethours();
intminutes = today.getminutes();
intseconds = today.getseconds();
if (inthours == 0) {
hours = "12:";
xfile = "午夜";
} else if (inthours < 12) {
hours = inthours+":";
xfile = "上午";
} else if (inthours == 12) {
hours = "12:";
xfile = "正午";
} else {
inthours = inthours - 12
hours = inthours + ":";
xfile = "下午";
}
if (intminutes < 10) {
minutes = "0"+intminutes+":";
} else {
minutes = intminutes+":";
}
if (intseconds < 10) {
seconds = "0"+intseconds+" ";
} else {
seconds = intseconds+" ";
}
timestring = theday+xfile+hours+minutes+seconds;
clock.innerhtml = timestring;
window.settimeout("tick();", 100);
}
window.onload = tick;
</script>
比较实用的代码,值得珍藏^_^