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();
            }
  • 相关阅读:
    如何让自己的app尽量不被系统杀死
    linux常用命令-权限管理命令
    linux常用命令-用户管理命令
    linux常用命令-文件处理命令
    npm命令
    新技术新框架新工具选型原则
    tomcat启动命令行中文乱码
    docker命令
    tinkpad e450c 进入 BIOS
    基于Java服务的前后端分离解决跨域问题
  • 原文地址:https://www.cnblogs.com/wangshengli520/p/10168553.html
Copyright © 2011-2022 走看看