Skip to content

Commit

Permalink
fix: Fix .format API returns UTC offset when value is 0 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed May 7, 2019
1 parent 86cd839 commit b254964
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,25 +302,25 @@ class Dayjs {
MMMM: months[$M] || months(this, str),
D: this.$D,
DD: Utils.s(this.$D, 2, '0'),
d: this.$W,
d: String(this.$W),
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
dddd: weekdays[this.$W],
H: $H,
H: String($H),
HH: Utils.s($H, 2, '0'),
h: get$H(1),
hh: get$H(2),
a: meridiemFunc($H, $m, true),
A: meridiemFunc($H, $m, false),
m: $m,
m: String($m),
mm: Utils.s($m, 2, '0'),
s: this.$s,
s: String(this.$s),
ss: Utils.s(this.$s, 2, '0'),
SSS: Utils.s(this.$ms, 3, '0'),
Z: zoneStr // 'ZZ' logic below
}

return String(str.replace(C.REGEX_FORMAT, (match, $1) => $1 || matches[match] || zoneStr.replace(':', ''))) // 'ZZ'
return str.replace(C.REGEX_FORMAT, (match, $1) => $1 || matches[match] || zoneStr.replace(':', '')) // 'ZZ'
}

utcOffset() {
Expand Down
13 changes: 7 additions & 6 deletions test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ it('Format Minute m mm', () => {
})

it('Format Second s ss SSS', () => {
// Todo: debug CI error
console.log(Date.now()) // eslint-disable-line no-console
console.log((new Date()).toString()) // eslint-disable-line no-console
console.log((new Date()).toLocaleString()) // eslint-disable-line no-console
console.log((new Date()).getTimezoneOffset()) // eslint-disable-line no-console
// debug
expect(dayjs().format('s')).toBe(moment().format('s'))
expect(dayjs().format('ss')).toBe(moment().format('ss'))
expect(dayjs().format('SSS')).toBe(moment().format('SSS'))
Expand Down Expand Up @@ -145,6 +139,13 @@ it('Format ddd dd MMM with short locale', () => {
.format('MMM'))
})

it('Format token value is 0', () => {
const sundayDate = '2000-01-02'
const sundayStr = 'd H m s'
expect(dayjs(sundayDate).format(sundayStr))
.toBe(moment(sundayDate).format(sundayStr))
})

it('Format Complex with other string - : / ', () => {
const string = 'YY-M-D / HH:mm:ss'
expect(dayjs().format(string)).toBe(moment().format(string))
Expand Down

0 comments on commit b254964

Please sign in to comment.