Javascript日期格式化指定格式的字符串实现

简介: 代码部分TypeScript 1 /** 2 * format a Date object 3 * 将 Date 转化为指定格式的String 4 * @param {Date} date 源日期对象 5 * @param {string...

代码部分

TypeScript

 1   /**
 2      * format a Date object 
 3      * 将 Date 转化为指定格式的String
 4      * @param {Date} date 源日期对象
 5      * @param {string} pattern 匹配模式
 6      * @returns {string} 格式化结果
 7      */
 8     fmtDate(date: Date, pattern: string) {
 9         return pattern
10             .replace(/yyyy/, date.getFullYear().toString())
11             .replace(/MM/, this.fillZero(date.getMonth() + 1, 'l', 2))
12             .replace(/dd/, this.fillZero(date.getDate(), 'l', 2))
13             .replace(/hh/, this.fillZero(date.getHours(), 'l', 2))
14             .replace(/mm/, this.fillZero(date.getMinutes(), 'l', 2))
15             .replace(/ss/, this.fillZero(date.getSeconds(), 'l', 2))
16             .replace(/S/, date.getMilliseconds().toString());
17     }
View Code

Javascript

 1     /**
 2      * format a Date object
 3      * 将 Date 转化为指定格式的String
 4      * @param {Date} date 源日期对象
 5      * @param {string} pattern 匹配模式
 6      * @returns {string} 格式化结果
 7      */
 8     Aqua.prototype.fmtDate = function (date, pattern) {
 9         return pattern
10             .replace(/yyyy/, date.getFullYear().toString())
11             .replace(/MM/, this.fillZero(date.getMonth() + 1, 'l', 2))
12             .replace(/dd/, this.fillZero(date.getDate(), 'l', 2))
13             .replace(/hh/, this.fillZero(date.getHours(), 'l', 2))
14             .replace(/mm/, this.fillZero(date.getMinutes(), 'l', 2))
15             .replace(/ss/, this.fillZero(date.getSeconds(), 'l', 2))
16             .replace(/S/, date.getMilliseconds().toString());
17     };
View Code

 

补零函数 Typescript

    /**
     * fill 0 to a number
     * 数字补零
     * @param {number} src 源数字
     * @param {string} direction 方向 l r
     * @param {number} digit 补零后的总位数
     * @returns {string} 结果
     */
    fillZero(src: number, direction: string, digit: number) {
        let count: number = digit - src.toString().length;
        let os = new Array(count + 1).join('0');
        if (direction !== 'r') {
            return os + src;
        }
        return src + os;
    }
View Code

javascript

    /**
     * fill 0 to a number
     * 数字补零
     * @param {number} src 源数字
     * @param {string} direction 方向 l r
     * @param {number} digit 补零后的总位数
     * @returns {string} 结果
     */
    Aqua.prototype.fillZero = function (src, direction, digit) {
        var count = digit - src.toString().length;
        var os = new Array(count + 1).join('0');
        if (direction !== 'r') {
            return os + src;
        }
        return src + os;
    };
View Code

 

原理很简单,就不写了

欢迎查看我的GitHub

https://github.com/rocketRobin/aqua-toolbox

目录
相关文章
|
1天前
|
JavaScript 数据处理 索引
js字符串截取
js字符串截取
8 2
|
5天前
|
存储 JavaScript 前端开发
JavaScript字符串方法详解
JavaScript字符串方法详解
16 0
|
6天前
|
SQL 缓存 JavaScript
深入解析JavaScript中的模板字符串
深入解析JavaScript中的模板字符串
13 1
|
6天前
|
JavaScript 索引
js字符串操作的方法
js字符串操作的方法
11 0
|
6天前
|
JavaScript 前端开发 索引
js关于字符串的方法
js关于字符串的方法
10 0
|
6天前
|
JavaScript 前端开发
js关于字符串的方法
js关于字符串的方法
10 0
|
6天前
|
开发框架 JavaScript .NET
Js字符串操作函数大全
Js字符串操作函数大全
10 1
|
6天前
|
JavaScript 前端开发 索引
js操作字符串的方法
js操作字符串的方法
14 2
|
13天前
|
JavaScript 前端开发 索引
JavaScript 数组的索引方法数组转换为字符串方法
JavaScript 数组的索引方法数组转换为字符串方法
|
13天前
|
编解码 JavaScript 前端开发
【专栏】介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例
【4月更文挑战第29天】本文介绍了字符串Base64编解码的基本原理和在Java、Python、C++、JavaScript及Go等编程语言中的实现示例。Base64编码将24位二进制数据转换为32位可打印字符,用“=”作填充。文中展示了各语言的编码解码代码,帮助开发者理解并应用于实际项目。