javascript 案例:模拟时钟

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <!--Javascript示例代码解释:这个示例用到了Javascript内置对象Date的getFullYear,
    getMonth和getDate方法。首先申明一个变量d,var d = new Date(),
    表示将当天的日期值赋给变量d。然后使用getFullYear得到年份的值,
    用getMonth得到月份值(注:getMonth返回值范围为0到11,所以要得到实际的月份,还要加1),
    用getDate得到当天日期所在月份的日期值。

    这个示例还用到了"test?语句1:语句2",意思是如果符合test条件,那么执行语句1,
    否则使用语句2。计算月和日都用到了这个语法,如果月和日小于10,在月和日的值之前应该加0。=-->
    <script type="text/javascript">
        function Format2Len(i) {
//            if (i < 10) {
//                return "0" + i;
//            }
//            else {
//                return i;
            //            }
            return i < 10 ? "0" + i : i;
        }
        function RefreshClock() {
            var CurrentTime = new Date();
            var timeStr = CurrentTime.getFullYear() + "-" +
                    Format2Len(CurrentTime.getMonth()+1) + "-" +
                    Format2Len(CurrentTime.getDate()) + " " +
                    Format2Len(CurrentTime.getHours()) + ":" +
                    Format2Len(CurrentTime.getMinutes()) + ":" +
                    Format2Len(CurrentTime.getSeconds());
            txtClock.value = timeStr;
        }
        setInterval("RefreshClock()", 1000);
    </script>
</head>
<body οnlοad="RefreshClock()">        <!--页面加载的时候执行一次-->
    当前时间:<input type="text" id="txtClock" />
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值