• 常用的时间处理(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;
        }
    }
    
  • 相关阅读:
    转C++的一点点
    无向图hash
    字符串相关
    Tutte矩阵求一般图最大匹配
    FFT的常数优化
    洲阁筛
    半平面交
    非旋treap套线段树
    点分治 [IOI2011]Race
    treap
  • 原文地址:https://www.cnblogs.com/cc-freiheit/p/13255276.html
Copyright © 2020-2023  润新知