zoukankan      html  css  js  c++  java
  • 常用的时间处理(Date => String)

    var timer = {
        /**
         * 补足 0
         * @param { Number | String } num 
         * @returns { String }
         */
        padLeftZero: function(num) {
            // es6 中可用 padStart() 来完成补足
            return ('0' + num).slice(-2);
        },
    
        /**
         * 处理成想要的日期字符串
         * @param { Date | String} date 
         * @param { String } formate 
         * @returns { String }
         */
        handle2String: function(date, formate) {
            if (date === null || date.length === 0) return '';
            
            var tempDate = new Date(date);
    
            if (/(y+)/.test(formate)) formate = formate.replace(RegExp.$1, (tempDate.getFullYear() + '').substring(4 - RegExp.$1.length));
            
            var o = {
                'M+': tempDate.getMonth() + 1,
                'd+': tempDate.getDate(),
                'h+': tempDate.getHours(),
                'm+': tempDate.getMinutes(),
                's+': tempDate.getSeconds(),
            }
    
            for (var key in o) {
                if (!o.hasOwnProperty(key)) continue;
    
                if (new RegExp(`(${key})`).test(formate)) formate = formate.replace(RegExp.$1, this.padLeftZero(o[key]));
            }
    
            return formate;
        }
    }
    
  • 相关阅读:
    Linux——端口命令
    Linux——iptables 禁止 IP和端口
    CE第9关共用
    获得程序窗体标题-FindWindowW需要的参数
    mysql ODBC win10 设置
    Work
    Pet
    Is It A Tree?
    Ice_cream's world I
    小希的迷宫
  • 原文地址:https://www.cnblogs.com/cc-freiheit/p/13255276.html
Copyright © 2011-2022 走看看