zoukankan      html  css  js  c++  java
  • 原生时钟代码

    /* 最简单的时钟 */
            function getTime() {
                var now = new Date();
                var result = {};
                result.year = getYear(now);
                result.month = repairWithZero(getMonth(now));
                result.day = repairWithZero(getDay(now));
                result.week = getWeek(now);
                result.hour = repairWithZero(getHours(now));
                result.minute = repairWithZero(getMinutes(now));
                result.second = repairWithZero(getSeconds(now));
                return result;
            }
            function formatTimeCh(obj) {
                var str = '';
                var weeks = ['日', '一', '二', '三', '四', '五', '六'];
                str =
                    `${obj.year}年${obj.month}月${obj.day}日星期${weeks[obj.week]} ${obj.hour}:${obj.minute}:${obj.second}`;
                document.getElementById('time').innerHTML = str;
            }
            function formatTimeEn(obj) {
                var str = '';
                var hourStr;
                var weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
                if (obj.hour > 12) {
                    hourStr = `${obj.hour-12}:${obj.minute}:${obj.second} PM`;
                } else {
                    hourStr = `${obj.hour}:${obj.minute}:${obj.second} AM`;
                }
                str = `${obj.year}-${obj.month}-${obj.day} ${weeks[obj.week]} ${hourStr}`;
                document.getElementById('etime').innerHTML = str;
            }
            function formatTime(obj) {
                formatTimeCh(obj);
                formatTimeEn(obj)
            }
            setInterval(function () {
                formatTime(getTime());
            }, 1000)
            function repairWithZero(num) {
                if (num < 0) {
                    return false;
                } else if (num < 10) {
                    return '0' + num;
                } else {
                    return num;
                }
            }
            function getYear(date) {
                return date.getFullYear();
            }
            function getMonth(date) {
                return date.getMonth() + 1;
            }
            function getDay(date) {
                return date.getDate();
            }
            function getWeek(date) {
                return date.getDay();
            }
            function getHours(date) {
                return date.getHours();
            }
            function getMinutes(date) {
                return date.getMinutes();
            }
            function getSeconds(date) {
                return date.getSeconds();
            }
  • 相关阅读:
    JAVA中分为基本数据类型及引用数据类型
    Tomcat部署HTTPS协议
    MySQL SQL 数据排名查询某条数据是总数据的第几条
    Myeclipse或Eclipse 老是出现JPA project Change Event Handler
    初识Go
    MyBatis xml文件中的大于、小于等符号写法
    jQuery实现5秒倒计时
    JS时间处理由CST格式转成GMT格式时间
    HTML新增加的属性和废除的属性
    HTML 锚点
  • 原文地址:https://www.cnblogs.com/wangshengli520/p/10168553.html
Copyright © 2011-2022 走看看