Skip to content

Commit

Permalink
feat(formatDate): 支持汉字的周几, 用 z 表示
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Apr 22, 2019
1 parent 0de580e commit ac66f0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/formatDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { toDate } from './toDate'

export type FormatDateValue = string | number | Date

export const dayToZhou: Record<number, string> = {
0: '日',
1: '一',
2: '二',
3: '三',
4: '四',
5: '五',
6: '六',
7: '日',
}

/**
* 格式化日期显示。
*
Expand All @@ -20,6 +31,7 @@ export function formatDate(value: FormatDateValue, template: string): string {
i: date.getMinutes(), // 分
s: date.getSeconds(), // 秒
l: date.getMilliseconds(), // 毫秒
z: dayToZhou[date.getDay()], // 汉字的周几
}
return formatTemplate(template, patternToValue)
}
5 changes: 5 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,11 @@ describe('formatDate', () => {
expect(vtils.formatDate(1541211914, 'yyyy年m月d日 h:i')).toBe(moment(1541211914 * 1000).format('YYYY年M月D日 H:m'))
expect(vtils.formatDate(now, 'yyyy年m月d日 h:i:s')).toBe(moment(now).format('YYYY年M月D日 H:m:s'))
})
test('周几', () => {
vtils.range(0, 7).forEach(i => {
expect(vtils.formatDate(`2019-4-${22 + i}`, '周z')).toBe(`周${vtils.dayToZhou[i + 1]}`)
})
})
})

describe('startsWith', () => {
Expand Down

0 comments on commit ac66f0d

Please sign in to comment.