当前位置: 代码迷 >> ASP.NET >> 在网页上可以显示当前时间的控件是?小弟我是初学者
  详细解决方案

在网页上可以显示当前时间的控件是?小弟我是初学者

热度:6378   发布时间:2013-02-25 00:00:00.0
在网页上可以显示当前时间的控件是?我是菜鸟~
可以用lable来作为显示当前时间的控件吗
比如显示“今天是2012年2月28日 星期二”
但是怎么与当前时间关联呢

谢大侠~


------解决方案--------------------------------------------------------

asp.net页放个Label,比如ID:lblNowDate

.cs文件Load事件中---->lblNowDate.Text = DateTime.Now.ToString();

当然时候格式化,可以根据你的需求自己来
------解决方案--------------------------------------------------------
探讨
ls说的差不多了 还得有个计时器 设置为1秒 定时刷新

------解决方案--------------------------------------------------------
计时器不一定要每秒都要更新啊,参数是1000的时候就是一秒,你可以更改这个参数。
如果你不添加计时器,就不能动态更新时间,也就是时间是不动的。
你可以用一个时间变量存储DateTime.Now()取下的时间,这时间变量里面年月日时分秒都有了,你可以按需求增减,在变量后一点就出来它的属性
------解决方案--------------------------------------------------------

添加个label,添加个timer
 this.lblStatus.Text = "系统当前时间:" + DateTime.Now.ToString();
 this.systime.Interval = 1000;//每个1s更新时间
写tick事件
private void systime_Tick(object sender, EventArgs e)
{
this.lblStatus.Text = "系统当前时间" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
}
------解决方案--------------------------------------------------------
C# code
<b>现在是:<span id="thetime"></span></b><script type="text/javascript">var timerID = null;var timerRunning = false;function stopclock() {    if (timerRunning)        clearTimeout(timerID);    timerRunning = false;}function startclock() {    stopclock();    showtime();}function showtime() {    var now = new Date();    var hours = now.getHours();    var minutes = now.getMinutes();    var seconds = now.getSeconds()    var timeValue = now.getFullYear() + "年" + (now.getMonth() + 1) + "月" + now.getDate() + "日 "    timeValue += hours    timeValue += ((minutes < 10) ? ":0" : ":") + minutes    timeValue += ((seconds < 10) ? ":0" : ":") + seconds    document.getElementById("thetime").innerHTML = timeValue;    timerID = setTimeout("showtime()", 1000);    timerRunning = true;}</script>
  相关解决方案