Skip to content

Commit

Permalink
feat: add formatDate
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 3, 2018
1 parent 8ef84ab commit e74fb0b
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/formatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import reduce from './reduce'
import repeat from './repeat'
import toDate from './toDate'

/**
* 格式化日期显示。
*
* @param value 要格式化的值,内部将用 `toDate` 处理为 `Date` 对象
* @param template 格式化模板
* @returns 格式化后的值
*/
export default function formatDate(value: string | number | Date, template: string): string {
const date = toDate(value)
const patterns: { [key: string]: number } = {
y: date.getFullYear(), // 年
m: date.getMonth() + 1, // 月
d: date.getDate(), // 日
h: date.getHours(), // 时
i: date.getMinutes(), // 分
s: date.getSeconds(), // 秒
l: date.getMilliseconds() // 毫秒
}
return reduce(patterns, (result, patternValue, patternKey) => {
const patternValueStr = String(patternValue)
const len = patternValueStr.length
return result.replace(
new RegExp(`(${patternKey}+)`, 'g'),
$0 => {
const n = $0.length
return (
n === 1 || n === len ? patternValueStr // n=1或n=len: 直接返回
: n < len ? patternValueStr.substr(len - n) // n<len: 截取后n位
: repeat(0, n - len) + patternValueStr // n>len: 前填充0到n位
)
}
)
}, template)
}

0 comments on commit e74fb0b

Please sign in to comment.