c#.net用JavaScript实现 时钟显示
原文发布时间为:2008-10-10 —— 来源于本人的百度文章 [由搬家工具导入]
显示日期,星期,以及时间:
JS文件夹与default.aspx在同一个文件夹下
JS文件夹中有date.js文件,文件内容为:
// JScript 文件
///显示时间
// JScript 文件
///显示时间
function getDate()
{
var d = new Date();
switch (d.getDay()) {
case 0:
strweek="日";
break;
case 1:
strweek="一";
break;
case 2:
strweek="二";
break;
case 3:
strweek="三";
break;
case 4:
strweek="四";
break;
case 5:
strweek="五";
break;
case 6:
strweek="六";
break;
}
document.write((d.getYear())+"年"+(d.getMonth()+1)+"月"+d.getDate()+ "日"+" 星期"+ strweek);
}
function getTime()
{
var d = new Date();
sec = d.getSeconds();
hr = d.getHours();
min = d.getMinutes();
if (hr <= 9) hr = "0" + hr;
if (min <= 9) min = "0" + min;
if (sec <= 9) sec = "0" + sec;
var t = " " + hr + ":" + min + ":" + sec;
document.getElementById("ShowTime").innerText = t;
}
function showTime()
{
if (document.all) setInterval("getTime()",1000)
}
defaul.aspx的源码为:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type="text/javascript" src="JS/date.js"></script>
</head>
<body onload="showTime();">
<form id="form1" runat="server">
<script type="text/javascript">getDate();</script>
<span id="ShowTime"></span>
<div>
</div>
</form>
</body>
</html>
参考资料:http://hi.baidu.com/anlaflasher/blog/item/39dc8e185ccb1e0234fa41ba.html
html文件代码可以这样写:
<html>
<head>
<title>显示时间</title>
<script language="JavaScript">
function check(val){
if (val<10){
return("0"+val);
}else{
return(val);
}
}
function ShowTime()
{
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth()+1;
var day=date.getDate();
var hour=date.getHours();
var minutes=date.getMinutes();
var second=date.getSeconds();
var timestr=year+"年"+month+"月"+day+"日 "+check(hour)+":"+check(minutes)+":"+check(second);
document.getElementById("stime").innerText=timestr;
var timerID=setTimeout('ShowTime();',1000);
}
</script>
</head>
<body onload="ShowTime();">
<span id="stime"></span>
</body>
</html>