Skip to content

Commit b254964

Browse files
committed
fix: Fix .format API returns UTC offset when value is 0 bug
1 parent 86cd839 commit b254964

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

src/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -302,25 +302,25 @@ class Dayjs {
302302
MMMM: months[$M] || months(this, str),
303303
D: this.$D,
304304
DD: Utils.s(this.$D, 2, '0'),
305-
d: this.$W,
305+
d: String(this.$W),
306306
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
307307
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
308308
dddd: weekdays[this.$W],
309-
H: $H,
309+
H: String($H),
310310
HH: Utils.s($H, 2, '0'),
311311
h: get$H(1),
312312
hh: get$H(2),
313313
a: meridiemFunc($H, $m, true),
314314
A: meridiemFunc($H, $m, false),
315-
m: $m,
315+
m: String($m),
316316
mm: Utils.s($m, 2, '0'),
317-
s: this.$s,
317+
s: String(this.$s),
318318
ss: Utils.s(this.$s, 2, '0'),
319319
SSS: Utils.s(this.$ms, 3, '0'),
320320
Z: zoneStr // 'ZZ' logic below
321321
}
322322

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

326326
utcOffset() {

test/display.test.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ it('Format Minute m mm', () => {
9898
})
9999

100100
it('Format Second s ss SSS', () => {
101-
// Todo: debug CI error
102-
console.log(Date.now()) // eslint-disable-line no-console
103-
console.log((new Date()).toString()) // eslint-disable-line no-console
104-
console.log((new Date()).toLocaleString()) // eslint-disable-line no-console
105-
console.log((new Date()).getTimezoneOffset()) // eslint-disable-line no-console
106-
// debug
107101
expect(dayjs().format('s')).toBe(moment().format('s'))
108102
expect(dayjs().format('ss')).toBe(moment().format('ss'))
109103
expect(dayjs().format('SSS')).toBe(moment().format('SSS'))
@@ -145,6 +139,13 @@ it('Format ddd dd MMM with short locale', () => {
145139
.format('MMM'))
146140
})
147141

142+
it('Format token value is 0', () => {
143+
const sundayDate = '2000-01-02'
144+
const sundayStr = 'd H m s'
145+
expect(dayjs(sundayDate).format(sundayStr))
146+
.toBe(moment(sundayDate).format(sundayStr))
147+
})
148+
148149
it('Format Complex with other string - : / ', () => {
149150
const string = 'YY-M-D / HH:mm:ss'
150151
expect(dayjs().format(string)).toBe(moment().format(string))

0 commit comments

Comments
 (0)