JavaScript—获取本地时间以12小时制显示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取时间</title>
    <script type="text/javascript">
        function shijian() {
            var today = new Date();
            var hh = today.getHours();
            var mm = today.getMinutes();
            var ss = today.getSeconds();
            var day = today.getDay();
            var date = today.getDate();
            var year = today.getFullYear();
            var month = today.getMonth() + 1;

            /*声明一个变量来接收转换成字符串*/

            var dayday;

            /*用switch完成转换操作*/
            switch (day) {
                case 1:
                    dayday = "一";
                    break;
                case 2:
                    dayday = "二";
                    break;
                case 3:
                    dayday = "三";
                    break;
                case 4:
                    dayday = "四";
                    break;
                case 5:
                    dayday = "五";
                    break;
                case 6:
                    dayday = "六";
                    break;
                case 7:
                    dayday = "日";
                    break;
            }

            /*声明AM和PM分别代表上午和下午,因为时间设置为12小时制,从用户角度出发,提供便利*/

            var apm = "PM";
            var abm = "AM";

            /*从系统获取的时间为24小时制,我们减去12就可以转换成12小时制*/

            if (hh > 12) {
                var num = hh - 12;
                document.getElementById("abc").innerHTML = "现在的系统时间:" + "<br/><br/>" + year + "年" + month + "月" + date + "日"
                    + num + "时" + mm + "分" + ss + "秒" + "&nbsp;" + "星期" + dayday + "&nbsp;" + "&nbsp;" + apm + "<br/><br/>" + "下午好!欢迎来到TB128!";
            } else {
                document.getElementById("abc").innerHTML = "现在的系统时间:" + "<br/><br/>" + year + "年" + month + "月" + date + "日"
                    + hh + "时" + mm + "分" + ss + "秒" + "&nbsp;" + "星期" + dayday + "&nbsp;" + "&nbsp;" + abm + "<br/><br/>" + "下午好!欢迎来到TB128!";
            }
        }

        /*每1000毫秒(1秒)执行一次shijian()方法*/
        
        setInterval("shijian()", 1000)
    </script>
</head>
<body>
<div id="abc">
    <input type="button" value="查看系统时间" οnclick="shijian()">
</div>
</body>
</html>

 

转载于:https://www.cnblogs.com/ichimoku/p/7810994.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值