页面显示当前时间(按秒增加时间)
方法一(JavaScript实现):
第一步在页面上放一个Div或者Span ,<span id ="clock"></span>
第二步:实现JavaScript(放在body后面)
<script> var clock = new Clock(); clock.display(document.getElementById("clock")); function Clock() { var date = new Date(); this.year = date.getFullYear(); this.month = date.getMonth() + 1; this.date = date.getDate(); this.day = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六")[date.getDay()]; this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds(); this.toString = function() { return "现在是:" + this.year + "年" + this.month + "月" + this.date + "日 " + this.hour + ":" + this.minute + ":" + this.second + " " + this.day; }; this.toSimpleDate = function() { return this.year + "-" + this.month + "-" + this.date; }; this.toDetailDate = function() { return this.year + "-" + this.month + "-" + this.date + " " + this.hour + ":" + this.minute + ":" + this.second; }; this.display = function(ele) { var clock = new Clock(); ele.innerHTML = clock.toString(); window.setTimeout(function() {clock.display(ele);}, 1000);//没秒刷新一次 }; } </script>
2,Asp后台实现
在页面上加入几个控件:Lable,Timer,ScriptManager,UpdatePannel,具体代码如下
<body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:Label ID="labClock" runat="server"></asp:Label> <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" /> </ContentTemplate> </asp:UpdatePanel> </form> </body>
注意这里Timer一定放在UpdatePanel内。否则是全页面刷新
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.Timer1.Interval = 1000; this.Timer1.Enabled = true; } } protected void Timer1_Tick(object sender, EventArgs e) { this.labClock.Text ="现在时间:"+DateTime.Now.ToLongDateString()+ DateTime.Now.ToLongTimeString(); }
--------------------------------------------------------------------------
关于时间:个人总结下
DateTime.ToString("yyyy-MM-dd HH:mm:ss");
' 以系统设置的长时间格式返回当前系统时间。
MyStr = Format(Time, "Long Time")
' 以系统设置的长日期格式返回当前系统日期。
MyStr = Format(Date, "Long Date")
MyStr = Format(MyTime, "h:m:s") ' 返回 "17:4:23"。
MyStr = Format(MyTime, "hh:mm:ss AMPM") ' 返回 "05:04:23 PM"。
MyStr = Format(MyDate, "dddd, mmm d yyyy") ' 返回 "Wednesday, Jan 27 1993"。
1、DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)
1.9 取中文日期显示——年月日时分
string strY=currentTime.ToString("f"); //不显示秒
1.10 取中文日期显示_年月
string strYM=currentTime.ToString("y");
1.11 取中文日期显示_月日
string strMD=currentTime.ToString("m");
1.12 取中文年月日
string strYMD=currentTime.ToString("D");
1.13 取当前时分,格式为:14:24
string strT=currentTime.ToString("t");
1.14 取当前时间,格式为:2003-09-23T14:46:48
string strT=currentTime.ToString("s");
1.15 取当前时间,格式为:2003-09-23 14:48:30Z
string strT=currentTime.ToString("u");
1.16 取当前时间,格式为:2003-09-23 14:48
string strT=currentTime.ToString("g");
1.17 取当前时间,格式为:Tue, 23 Sep 2003 14:52:40 GMT
string strT=currentTime.ToString("r");
1.18获得当前时间 n 天后的日期时间
DateTime newDay = DateTime.Now.AddDays(100);