From f3ef705613af83333fe132b470896a65e12f31b0 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 18 Jul 2024 19:37:49 +0800 Subject: [PATCH] fix: add UTC support to negativeYear plugin (#2692) --- src/plugin/negativeYear/index.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js index 6a3fbea7a..6fc90c06a 100644 --- a/src/plugin/negativeYear/index.js +++ b/src/plugin/negativeYear/index.js @@ -2,10 +2,16 @@ export default (_, c, dayjs) => { const proto = c.prototype const parseDate = (cfg) => { - const { date } = cfg + const { date, utc } = cfg if (typeof date === 'string' && date.charAt(0) === '-') { - const newDate = new Date(date.slice(1)) - const fullYear = newDate.getFullYear() + const normalData = date.slice(1) + let newDate = dayjs(normalData) + if (utc) { + newDate = dayjs.utc(normalData) + } else { + newDate = dayjs(normalData) + } + const fullYear = newDate.year() if (date.indexOf(`-${fullYear}`) !== -1) { return dayjs(newDate).subtract(fullYear * 2, 'year').toDate() }