Skip to content
Permalink
Browse files

fix: format allow escaping char

  • Loading branch information...
iamkun committed May 5, 2018
1 parent 547e8e1 commit c9c0abda7b4438cd4a592fb746fc7e2b6820ab87
Showing with 7 additions and 1 deletion.
  1. +1 −1 src/constant.js
  2. +1 −0 src/index.js
  3. +5 −0 test/display.test.js
@@ -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
@@ -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)
@@ -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'

0 comments on commit c9c0abd

Please sign in to comment.
You can’t perform that action at this time.