03月21, 2014

JS在网页上显示当前日期和时间

打开网页的时候,有时候我看到一句欢迎语句,然后显示当前的日期和时间. (兼容火狐与IE的),贴上代码:

<html>
<head>
    <title>JS在网页上显示当前日期和时间</title>
    <style type="text/css">
    * {
        margin: 0;
        padding: 0px;
    }
    #Clock {
        height: 40px;
        line-height: 40px;
        background: #73a3d4;
        text-indent: 2em;
        color: #fff;
        font-family: "微软雅黑";
    }
    </style>
    <script type="text/javascript">
    function tick() {
        var years, months, days, week, hours, minutes, seconds;;
        var intYears, intMonths, intDays, intHours, intMinutes, intSeconds;;
        var today;
        today = new Date(); //系统当前时间
        intYears = today.getFullYear(); //得到年份,getFullYear()比getYear()更普适
        intMonths = today.getMonth() + 1; //得到月份,要加1
        intDays = today.getDate(); //得到日期
        intHours = today.getHours(); //得到小时
        intMinutes = today.getMinutes(); //得到分钟
        intSeconds = today.getSeconds(); //得到秒钟
        switch (today.getDay()) {
            case 1:
                week = "星期一";
                break;
            case 2:
                week = "星期二";
                break;
            case 3:
                week = "星期三";
                break;
            case 4:
                week = "星期四";
                break;
            case 5:
                week = "星期五";
                break;
            case 6:
                week = "星期六";
                break;
            default:
                week = "星期天";
        }
        years = intYears + "年";
        if (intMonths < 10) {
            months = "0" + intMonths + "月";
        } else {
            months = intMonths + "月";
        }
        if (intDays < 10) {
            days = "0" + intDays + "日";
        } else {
            days = intDays + "日";
        }
        if (intHours == 0) {
            hours = "00:";
        } else if (intHours < 10) {
            hours = "0" + intHours + ":";
        } else {
            hours = intHours + ":";
        }
        if (intMinutes < 10) {
            minutes = "0" + intMinutes + ":";
        } else {
            minutes = intMinutes + ":";
        }
        if (intSeconds < 10) {
            seconds = "0" + intSeconds + " ";
        } else {
            seconds = intSeconds + " ";
        }
        timeString = years + months + days + ' ' + week + ' ' + hours + minutes + seconds;
        Clock.innerHTML = "您好,今天是:" + timeString;
        window.setTimeout("tick();", 1000);
    }
    window.onload = tick;
    </script>
</head>
<body>
    <div id="Clock"></div>
</body>
</html>

本文链接:https://901web.com/post/js在网页上显示当前日期和时间.html

-- EOF --

Comments

请在后台配置评论类型和相关的值。