Skip to content

Commit

Permalink
fix: format allow escaping char
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed May 5, 2018
1 parent 547e8e1 commit c9c0abd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'

// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{2})-?(\d{1,2})$/
export const REGEX_FORMAT = /Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
export const REGEX_FORMAT = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ class Dayjs {
format(formatStr) {
const str = formatStr || C.FORMAT_DEFAULT
return str.replace(C.REGEX_FORMAT, (match) => {
if (match.indexOf('[') > -1) return match.replace(/\[|\]/g, '')
switch (match) {
case 'YY':
return String(this.$y).slice(-2)
Expand Down
5 changes: 5 additions & 0 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ it('Format Complex with other string - : / ', () => {
expect(dayjs().format(string)).toBe(moment().format(string))
})

it('Format Escaping characters', () => {
const string = '[Z] Z'
expect(dayjs().format(string)).toBe(moment().format(string))
})

describe('Difference', () => {
it('empty -> default milliseconds', () => {
const dateString = '20110101'
Expand Down

0 comments on commit c9c0abd

Please sign in to comment.