动态显示当前时间(js)

来源:互联网 发布:淘宝店名能改几次 编辑:程序博客网 时间:2024/05/19 02:41

方法一:

    现在时刻: <span id="t01"></span>

 <script type="text/javascript">

 function gettime()

{

document.getElementById('t01').innerHTML=new Date().toLocaleString();

 }

setInterval(gettime,1000);

</script>

方法二:

<%@ 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 language="JavaScript" 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 date=now.toLocaleDateString();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds()
        //var timeValue = "" +((hours >= 12) ? "下午 " : "上午 " )
        //timeValue += ((hours >12) ? hours -12 :hours)
        var timeValue =date+" "+hours;
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes ;
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds ;
        document.getElementById("txtTime").value = timeValue;
        timerID = setTimeout("showtime()",1000);
        timerRunning = true;
        }

        </script>


</head>
<body onload="startclock()">
    <form id="form1" runat="server">
    <div>
     <span style="font-size:14px"> 当前时间:</span>
     <input name="txtTime" style="font-size: 9pt;color:#000000;border:0; width: 145px; height: 19px;" id="txtTime" readonly="readOnly" />
    </div>
    </form>

 </body>
</html> 

预览效果:


原创粉丝点击