Repository: iamkun/dayjs
Pull request #2640 (2024-04-26 16:39:48)

feat: Add negative years support 
 Add ability to parse negative years

This fix [2636](https://github.com/iamkun/dayjs/issues/2636) issue

willismonroe: This looks good, I added a test (in manipulate.test.js):
```
it('Add and subtract with negative years', () => {
  expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005'))
  expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007'))
})
```
Just to make sure that adding/subtracting were working well. I tried to commit to your fork, but it didn't work.
klm-turing: I added those test for you
![Screenshot from 2024-04-26 17-41-41](https://github.com/iamkun/dayjs/assets/167549638/9dfcf541-95fe-4ddc-b56a-8083d27afdb3)


willismonroe: That looks good!

Patch:
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index 34cbdac..4f1bc24 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -47,4 +47,6 @@ jobs:
           npm install -g @semantic-release/changelog @semantic-release/git semantic-release 
           GITHUB_REF=${{ env.GITHUB_TRIGGER_REF }} semantic-release
           echo release success
+      - name: Notify
+        run: |
           curl -s ${{ secrets.GITEE_SYNC_URL }}
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1447804..3391369 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,13 @@
+## [1.11.11](https://github.com/iamkun/dayjs/compare/v1.11.10...v1.11.11) (2024-04-28)
+
+
+### Bug Fixes
+
+* day of week type literal ([#2630](https://github.com/iamkun/dayjs/issues/2630)) ([f68d73e](https://github.com/iamkun/dayjs/commit/f68d73efe562fdedd9e288ecb0ce6565e602f507))
+* improve locale "zh-hk" format and meridiem ([#2419](https://github.com/iamkun/dayjs/issues/2419)) ([a947a51](https://github.com/iamkun/dayjs/commit/a947a5171aad5695eaf593bc95fe073de0f0894a))
+* Update 'da' locale to match correct first week of year ([#2592](https://github.com/iamkun/dayjs/issues/2592)) ([44b0936](https://github.com/iamkun/dayjs/commit/44b0936ad709212b63e48672d8b9c225e2c3b830))
+* update locale Bulgarian monthsShort Jan ([#2538](https://github.com/iamkun/dayjs/issues/2538)) ([f0c9a41](https://github.com/iamkun/dayjs/commit/f0c9a41c6ec91528f3790e442b0c5dff15a4e640))
+
 ## [1.11.10](https://github.com/iamkun/dayjs/compare/v1.11.9...v1.11.10) (2023-09-19)
 
 
diff --git a/README.md b/README.md
index 7b2fa9a..b50a700 100644
--- a/README.md
+++ b/README.md
@@ -6,7 +6,7 @@ English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/R
 <p align="center">Fast <b>2kB</b> alternative to Moment.js with the same modern API</p>
 <p align="center">
     <a href="https://unpkg.com/dayjs/dayjs.min.js"><img
-            src="https://img.badgesize.io/https://unpkg.com/dayjs/dayjs.min.js?compression=gzip&style=flat-square"
+            src="https://img.shields.io/bundlephobia/minzip/dayjs?style=flat-square&color=%2345cc11"
             alt="Gzip Size"></a>
     <a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square&colorB=51C838"
                                                        alt="NPM Version"></a>
diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js
new file mode 100644
index 0000000..6a3fbea
--- /dev/null
+++ b/src/plugin/negativeYear/index.js
@@ -0,0 +1,23 @@
+export default (_, c, dayjs) => {
+  const proto = c.prototype
+
+  const parseDate = (cfg) => {
+    const { date } = cfg
+    if (typeof date === 'string' && date.charAt(0) === '-') {
+      const newDate = new Date(date.slice(1))
+      const fullYear = newDate.getFullYear()
+      if (date.indexOf(`-${fullYear}`) !== -1) {
+        return dayjs(newDate).subtract(fullYear * 2, 'year').toDate()
+      }
+      return date
+    }
+    return date
+  }
+
+  const oldParse = proto.parse
+  proto.parse = function (cfg) {
+    cfg.date = parseDate.bind(this)(cfg)
+    oldParse.bind(this)(cfg)
+  }
+}
+
diff --git a/types/index.d.ts b/types/index.d.ts
index 766bd79..cd159dc 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -114,7 +114,7 @@ declare namespace dayjs {
      * ```
      * Docs: https://day.js.org/docs/en/get-set/day
      */
-    day(): number
+    day(): 0 | 1 | 2 | 3 | 4 | 5 | 6
     /**
      * Set the day of the week.
      *
diff --git a/types/plugin/negativeYear.d.ts b/types/plugin/negativeYear.d.ts
new file mode 100644
index 0000000..43bb868
--- /dev/null
+++ b/types/plugin/negativeYear.d.ts
@@ -0,0 +1,4 @@
+import {PluginFunc} from 'dayjs'
+
+declare const plugin: PluginFunc
+export = plugin
\ No newline at end of file


Test patch:
diff --git a/test/plugin/negativeYear.test.js b/test/plugin/negativeYear.test.js
new file mode 100644
index 0000000..a9713fa
--- /dev/null
+++ b/test/plugin/negativeYear.test.js
@@ -0,0 +1,52 @@
+import MockDate from 'mockdate'
+import dayjs from 'dayjs'
+import negativeYear from '../../src/plugin/negativeYear'
+import utc from '../../src/plugin/utc'
+import { REGEX_PARSE } from '../../src/constant'
+
+
+dayjs.extend(negativeYear)
+dayjs.extend(utc)
+
+beforeEach(() => {
+  MockDate.set(new Date())
+})
+
+afterEach(() => {
+  MockDate.reset()
+})
+
+describe('negativeYear', () => {
+  it('parses negative years', () => {
+    expect(dayjs('-2020-01-01').year()).toBe(-2020)
+    const date = '-2021/01/03'
+    const date2 = '01/03/-2021'
+    const date3 = '01-03--2021'
+    const d = date.match(REGEX_PARSE)
+    expect(dayjs(date).format('YYYY-MM-DD')).toBe('-2021-01-03')
+    expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date')
+    expect(dayjs(date3).format()).toBe('Invalid Date')
+    expect(d).toBe(null)
+  })
+
+  it('does not parse non-negative years', () => {
+    expect(dayjs('2020-01-01').year()).toBe(2020)
+  })
+
+  it('works with other plugins', () => {
+    expect(dayjs.utc('-2020-01-01').year()).toBe(-2020)
+  })
+
+  it('Add and subtract with negative years', () => {
+    expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005'))
+    expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007'))
+    expect(dayjs('-2006').add(1, 'y').format('YYYY')).toBe(dayjs('-2005').format('YYYY'))
+    expect(dayjs('-2006').subtract(1, 'y').format('YYYY')).toBe(dayjs('-2007').format('YYYY'))
+  })
+
+  it('Compare date with negative years', () => {
+    expect(dayjs('-2006').isAfter(dayjs('-2007'))).toBeTruthy()
+    expect(dayjs('-2006').isBefore(dayjs('-2005'))).toBeTruthy()
+    expect(dayjs('-2006').isSame('-2006')).toBeTruthy()
+  })
+})