Skip to content

Commit

Permalink
support ios 'YYYY-MM-DD HH:mm' formation. (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
AxeMea authored and iamkun committed Feb 4, 2019
1 parent 73eb122 commit 58a4d78
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/constant.js
Expand Up @@ -26,7 +26,7 @@ export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
export const INVALID_DATE_STRING = 'Invalid Date'

// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/
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 en = {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -56,7 +56,7 @@ const parseDate = (date) => {
// 2018-08-08 or 20180808
return new Date(
reg[1], reg[2] - 1, reg[3] || 1,
reg[5] || 0, reg[6] || 0, reg[7] || 0, reg[8] || 0
reg[4] || 0, reg[5] || 0, reg[6] || 0, reg[7] || 0
)
}
return new Date(date) // timestamp
Expand Down
4 changes: 4 additions & 0 deletions test/parse.test.js
Expand Up @@ -21,12 +21,16 @@ describe('Parse', () => {
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-04-24'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-04-24 11:12'
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
d = '2018-05-02 11:12:13'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-05-02 11:12:13.998'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-4-1'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018-4-1 11:12'
expect(dayjs(d).format()).toBe(moment(d).format()) // not recommend
d = '2018-4-1 1:1:1:223'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018-01'
Expand Down

0 comments on commit 58a4d78

Please sign in to comment.