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;
        }
    }
    
  • 相关阅读:
    9月9号作业
    9月9号笔记
    jupyter的补充
    jupyter的使用
    9月6号作业
    编程语言的分类
    计算机组成
    计算机组成的补充
    面向对象基础
    9月2号作业
  • 原文地址:https://www.cnblogs.com/cc-freiheit/p/13255276.html
Copyright © 2011-2022 走看看