代码部分

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

相关文章: