From b254964c184f06f0b10f1fb83568e45da2a620c9 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 8 May 2019 02:28:36 +0800 Subject: [PATCH] fix: Fix `.format` API returns UTC offset when value is 0 bug --- src/index.js | 10 +++++----- test/display.test.js | 13 +++++++------ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 3e0b5e141..03359c2d9 100644 --- a/src/index.js +++ b/src/index.js @@ -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() { diff --git a/test/display.test.js b/test/display.test.js index 145e068fc..c8fe19350 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -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')) @@ -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))