求一段WEB页面显示时间的代码

liulangdeyuyu 2010-09-07 05:43:54
如题

我能够让它显示时间,但是时间不更新

给一段能够更新的显示时间的代码


JavaScript脚本的也行
...全文
364 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhtchina 2010-09-07
  • 打赏
  • 举报
回复
<script language="JavaScript">
<!--

function Time(){
if (!document.layers&&!document.all)
return
var Timer=new Date()
var hours=Timer.getHours()
var minutes=Timer.getMinutes()
var seconds=Timer.getSeconds()
var noon="AM"
if (hours>12){
noon="PM"
hours=hours-12
}
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//change font size here to your desire
myclock="<font size='4' face='Arial' color=blue>"+hours+":"+minutes+":"
+seconds+" "+noon+"</b></font>"
if (document.layers){
document.layers.position.document.write(myclock)
document.layers.position.document.close()
}
else if (document.all)
position.innerHTML=myclock
setTimeout("Time()",1000)
}
//-->
</script>
<span id="position" style="position:absolute;left:441px;top:190px; width: 128px; height: 30px">
</span>

详细解释:
function Time() 定义一个函数。
{ if (!document.layers&&!document.all)
return 由于IE与Netscape对JavaScript的解释不同,造成浏览的效果不同,所以要分别写代码。这句话判断一下用户所使用的浏览器,如果两者都不是,就返回。
var timer=new Date() 定义一个新的变量,名字为timer,为一个新的Date的对象。
var hours=Timer.getHours()
var minutes=Timer.getMinutes()
var seconds=Timer.getSeconds() 分别定义3个变量,获得当前“小时”,“分钟”,“秒”的值。
var noon="AM" if (hours>12)
{ noon="PM" hours=hours-12 }
if (hours==0)
hours=12 定义一个名为“noon”的变量,当“小时”数大于12时,其值为PM,同时所得值减12;当“小时”数小于12时,其值为AM。
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds 如果“分钟”数或“秒”数小于9,则在前面加一个“0”。
myclock="<font color=blue>"+hours+":"+minutes+":" +seconds+" "+noon+"</b></font>" 用一个新变量把“小时,分,秒”结合起来。
if (document.layers)
{ document.layers.position.
document.write(myclock)
document.layers.position.document.close() } 如果浏览器是Netscape,就输出myclock,同时用于IE的代码就停止执行。
else if (document.all)
position.innerHTML=myclock 否则,浏览器是IE,就输出myclock。
setTimeout("Time()",1000) 每1000毫秒,调用一次Time函数,即一秒动一次。
onload="Time()" 页面装载时,调用Time()函数。
cuixia1986 2010-09-07
  • 打赏
  • 举报
回复

calendar = new Date();
day = calendar.getDay();
month = calendar.getMonth();
date = calendar.getDate();
year = calendar.getYear();
if (year< 100) year = 1900 + year;
cent = parseInt(year/100);
g = year % 19;
k = parseInt((cent - 17)/25);
i = (cent - parseInt(cent/4) - parseInt((cent - k)/3) + 19*g + 15) % 30;
i = i - parseInt(i/28)*(1 - parseInt(i/28)*parseInt(29/(i+1))*parseInt((21-g)/11));
j = (year + parseInt(year/4) + i + 2 - cent + parseInt(cent/4)) % 7;
l = i - j;
emonth = 3 + parseInt((l + 40)/44);
edate = l + 28 - 31*parseInt((emonth/4));
emonth--;
var dayname = new Array ("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var monthname =
new Array ("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月" );
document.write("当前时间:"+year +"年 ");
document.write(monthname[month]);
document.write(date + "日"+" ");
document.write(dayname[day]+" ");
document.write("<span id='clock'></span>");
var now,hours,minutes,seconds,timeValue;
function showtime(){
now = new Date();
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
timeValue = (hours >= 12) ? " 下午 " : " 上午 ";
timeValue += ((hours > 12) ? hours - 12 : hours) + "点";
timeValue += ((minutes <10)?"0":"") + minutes+"分";
timeValue += ((seconds <10)?"0":"") + seconds+"秒";
clock.innerHTML = timeValue;
setTimeout("showtime()",100);
}
showtime();



输出结果样式为 当前时间:2010年 9月7日 星期二 下午 8点14分26秒 可以根据需要修改颜色
龟仙 2010-09-07
  • 打赏
  • 举报
回复
BAIDU
GOOGLE
。。。。。。
程序员应该熟练应用。。
纯唇Yu弄 2010-09-07
  • 打赏
  • 举报
回复
setTimeout
wuyq11 2010-09-07
  • 打赏
  • 举报
回复
function showtime()
{
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
var m2="";
var s2="";
if(m<10)m2="0"+m;
else m2=m;
if(s<10)s2="0"+s;
else s2=s;
var txt = h+":"+m2+":"+s2;
var p = document.getElementById("txt_time");
p.value="现在时刻:"+txt;

setTimeout("showtime()",1000);
}
lchy110 2010-09-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 xuxiaomei2707 的回复:]
function www_helpor_net() {
var Digital = new Date()
var hours = Digital.getHours()
var minutes = Digital.getMinutes()
var seconds = Digital.getSeconds()

if (minutes <= 9)
minutes ……
[/Quote]

顶楼上
关键代码就是 setTimeout("www_helpor_net()", 1000)
这个意思就是间隔1秒钟就去执行这个www_helpor_net()方法 你在这个方法里通过JS 给一个lable赋值 就达到你要的效果了
xuxiaomei2707 2010-09-07
  • 打赏
  • 举报
回复
function www_helpor_net() {
var Digital = new Date()
var hours = Digital.getHours()
var minutes = Digital.getMinutes()
var seconds = Digital.getSeconds()

if (minutes <= 9)
minutes = "0" + minutes
if (seconds <= 9)
seconds = "0" + seconds
myclock = "<font color='#ffffff'>" + hours + ":" + minutes + ":" + seconds + "</font>"
if (document.layers) {
document.layers.liveclock.document.write(myclock)
document.layers.liveclock.document.close()
} else if (document.all)
liveclock.innerHTML = myclock
setTimeout("www_helpor_net()", 1000)
}

function DateDemo() {
var d, day, x, s = " ";
var x = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
d = new Date();
day = d.getDay();
return (s += x[day]);
}

window.onload = function() {
www_helpor_net();
document.getElementById("ShowTime").innerHTML = " <font color='#ffffff'>今天是:<%=showtime %>  " + DateDemo() + "</font>  ";
}
喜-喜 2010-09-07
  • 打赏
  • 举报
回复

private void MainFrm_Load(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
this.Label1.Text = "当前时间:" + System.DateTime.Now.ToLongDateString();
}
Ghost_Khz 2010-09-07
  • 打赏
  • 举报
回复
但是, 这样会刷新整个页面, 你可以试着把时间放在另外一个页面里面,然后用iframe嵌进来,只刷新这个时间的,效果会好点。
Ghost_Khz 2010-09-07
  • 打赏
  • 举报
回复
一秒种刷下页面, 就OK了。
liulangdeyuyu 2010-09-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ycagri 的回复:]

哦,要刷新呀,放个Timer上去,就可以刷新了

那用javascript setTimeout()
[/Quote]
能具体点吗??

Ghost_Khz 2010-09-07
  • 打赏
  • 举报
回复
加个Meta标签
liulangdeyuyu 2010-09-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ycagri 的回复:]

DateTime.Now
[/Quote]
这个我知道,但是怎么让它更新
ycagri 2010-09-07
  • 打赏
  • 举报
回复
哦,要刷新呀,放个Timer上去,就可以刷新了

那用javascript setTimeout()
ycagri 2010-09-07
  • 打赏
  • 举报
回复
DateTime.Now

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧