Repository: iamkun/dayjs
Pull request #2287 (2023-04-17 06:59:59)

D2M


Patch:
diff --git a/README.md b/README.md
index 277423a..ca0443f 100644
--- a/README.md
+++ b/README.md
@@ -112,6 +112,14 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
   <img width="70" src="https://avatars.githubusercontent.com/u/18175329?s=52&v=4">
 </a>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="https://opencollective.com/sight-and-sound-ministries" target="_blank">
+  <img width="70" src="https://user-images.githubusercontent.com/17680888/232316426-cb99b4cf-0ccb-4e73-a6ce-e16dba6aadf4.png">
+</a>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+<a href="https://www.exoflare.com/open-source/?utm_source=dayjs&utm_campaign=open_source" target="_blank">
+  <img width="70" src="https://user-images.githubusercontent.com/17680888/162761622-1407a849-0c41-4591-8aa9-f98114ec2092.png">
+</a>
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <a href="https://rxdb.info/?utm_source=day.js.org&utm_medium=banner&utm_campaign=day.js.org-sponsored" target="_blank"><img width="70" src="https://user-images.githubusercontent.com/17680888/200301812-9c9bd523-5dc4-4cab-b380-543fbcd3802c.svg"></a>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <a href="https://github.com/vendure-ecommerce" target="_blank"><img width="70" src="https://avatars.githubusercontent.com/u/39629390?s=52&v=4"></a>
diff --git a/src/index.js b/src/index.js
index 19c4090..ba74b8b 100644
--- a/src/index.js
+++ b/src/index.js
@@ -280,7 +280,7 @@ class Dayjs {
 
     const matches = {
       YY: String(this.$y).slice(-2),
-      YYYY: this.$y,
+      YYYY: Utils.s(this.$y, 4, '0'),
       M: $M + 1,
       MM: Utils.s($M + 1, 2, '0'),
       MMM: getShort(locale.monthsShort, $M, months, 3),
diff --git a/src/plugin/duration/index.js b/src/plugin/duration/index.js
index 10ebb4c..5d4cc6a 100644
--- a/src/plugin/duration/index.js
+++ b/src/plugin/duration/index.js
@@ -235,6 +235,10 @@ class Duration {
       .fromNow(!withSuffix)
   }
 
+  valueOf() {
+    return this.asMilliseconds()
+  }
+
   milliseconds() { return this.get('milliseconds') }
   asMilliseconds() { return this.as('milliseconds') }
   seconds() { return this.get('seconds') }
diff --git a/types/index.d.ts b/types/index.d.ts
index 1ac6c8d..766bd79 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -382,7 +382,7 @@ declare namespace dayjs {
      *
      * Docs: https://day.js.org/docs/en/query/is-before
      */
-    isBefore(date: ConfigType, unit?: OpUnitType): boolean
+    isBefore(date?: ConfigType, unit?: OpUnitType): boolean
     /**
      * This indicates whether the Day.js object is the same as the other supplied date-time.
      * ```
@@ -394,7 +394,7 @@ declare namespace dayjs {
      * ```
      * Docs: https://day.js.org/docs/en/query/is-same
      */
-    isSame(date: ConfigType, unit?: OpUnitType): boolean
+    isSame(date?: ConfigType, unit?: OpUnitType): boolean
     /**
      * This indicates whether the Day.js object is after the other supplied date-time.
      * ```
@@ -408,7 +408,7 @@ declare namespace dayjs {
      *
      * Docs: https://day.js.org/docs/en/query/is-after
      */
-    isAfter(date: ConfigType, unit?: OpUnitType): boolean
+    isAfter(date?: ConfigType, unit?: OpUnitType): boolean
 
     locale(): string
 
diff --git a/types/plugin/isSameOrAfter.d.ts b/types/plugin/isSameOrAfter.d.ts
index 1c8c264..916bc80 100644
--- a/types/plugin/isSameOrAfter.d.ts
+++ b/types/plugin/isSameOrAfter.d.ts
@@ -5,6 +5,6 @@ export = plugin
 
 declare module 'dayjs' {
   interface Dayjs {
-    isSameOrAfter(date: ConfigType, unit?: OpUnitType): boolean
+    isSameOrAfter(date?: ConfigType, unit?: OpUnitType): boolean
   }
 }
diff --git a/types/plugin/isSameOrBefore.d.ts b/types/plugin/isSameOrBefore.d.ts
index 1df5492..d52b095 100644
--- a/types/plugin/isSameOrBefore.d.ts
+++ b/types/plugin/isSameOrBefore.d.ts
@@ -5,6 +5,6 @@ export = plugin
 
 declare module 'dayjs' {
   interface Dayjs {
-    isSameOrBefore(date: ConfigType, unit?: OpUnitType): boolean
+    isSameOrBefore(date?: ConfigType, unit?: OpUnitType): boolean
   }
 }
diff --git a/types/plugin/isoWeek.d.ts b/types/plugin/isoWeek.d.ts
index 26c591b..3f4d88f 100644
--- a/types/plugin/isoWeek.d.ts
+++ b/types/plugin/isoWeek.d.ts
@@ -18,10 +18,10 @@ declare module 'dayjs' {
 
     endOf(unit: ISOUnitType): Dayjs
 
-    isSame(date: ConfigType, unit?: ISOUnitType): boolean
+    isSame(date?: ConfigType, unit?: ISOUnitType): boolean
 
-    isBefore(date: ConfigType, unit?: ISOUnitType): boolean
+    isBefore(date?: ConfigType, unit?: ISOUnitType): boolean
 
-    isAfter(date: ConfigType, unit?: ISOUnitType): boolean
+    isAfter(date?: ConfigType, unit?: ISOUnitType): boolean
   }
 }
diff --git a/types/plugin/quarterOfYear.d.ts b/types/plugin/quarterOfYear.d.ts
index 8a9cc1e..317e0ad 100644
--- a/types/plugin/quarterOfYear.d.ts
+++ b/types/plugin/quarterOfYear.d.ts
@@ -17,10 +17,10 @@ declare module 'dayjs' {
 
     endOf(unit: QUnitType | OpUnitType): Dayjs
 
-    isSame(date: ConfigType, unit?: QUnitType): boolean
+    isSame(date?: ConfigType, unit?: QUnitType): boolean
 
-    isBefore(date: ConfigType, unit?: QUnitType): boolean
+    isBefore(date?: ConfigType, unit?: QUnitType): boolean
 
-    isAfter(date: ConfigType, unit?: QUnitType): boolean
+    isAfter(date?: ConfigType, unit?: QUnitType): boolean
   }
 }
diff --git a/types/plugin/timezone.d.ts b/types/plugin/timezone.d.ts
index d504f69..049bb08 100644
--- a/types/plugin/timezone.d.ts
+++ b/types/plugin/timezone.d.ts
@@ -10,7 +10,7 @@ declare module 'dayjs' {
   }
 
   interface DayjsTimezone {
-    (date: ConfigType, timezone?: string): Dayjs
+    (date?: ConfigType, timezone?: string): Dayjs
     (date: ConfigType, format: string, timezone?: string): Dayjs
     guess(): string
     setDefault(timezone?: string): void


Test patch:
diff --git a/test/display.test.js b/test/display.test.js
index 7eb48fe..86a978d 100644
--- a/test/display.test.js
+++ b/test/display.test.js
@@ -260,3 +260,15 @@ it('As JSON -> toJSON', () => {
 it('As ISO 8601 String -> toISOString e.g. 2013-02-04T22:44:30.652Z', () => {
   expect(dayjs().toISOString()).toBe(moment().toISOString())
 })
+
+it('Year 1 formatted with YYYY should pad with zeroes', () => {
+  const date = new Date(1, 0, 1)
+  date.setUTCFullYear(1) // Required because 0-99 are parsed as 19xx in JS: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#year
+  expect(dayjs(date).format('YYYY')).toBe('0001')
+})
+
+it('Year 1 formatting matches moment format', () => {
+  const date = new Date(1, 0, 1)
+  date.setUTCFullYear(1)
+  expect(dayjs(date).format('YYYY')).toBe(moment(date).format('YYYY'))
+})
diff --git a/test/plugin/duration.test.js b/test/plugin/duration.test.js
index 2e80469..8a7e0e6 100644
--- a/test/plugin/duration.test.js
+++ b/test/plugin/duration.test.js
@@ -67,6 +67,9 @@ describe('Creating', () => {
       ms: -1
     }).toISOString()).toBe('-PT0.001S')
   })
+  it('convert to milliseconds', () => {
+    expect(+dayjs.duration(100)).toBe(100)
+  })
 })
 
 describe('Parse ISO string', () => {
diff --git a/test/plugin/isSameOrAfter.test.js b/test/plugin/isSameOrAfter.test.js
index fd504c2..f5533d7 100644
--- a/test/plugin/isSameOrAfter.test.js
+++ b/test/plugin/isSameOrAfter.test.js
@@ -49,6 +49,13 @@ test('is same or after without units', () => {
   expect(+m).toEqual(+mCopy, 'isSameOrAfter second should not change moment')
 })
 
+test('is same or after without date', () => {
+  const past = dayjs().subtract(1, 'day')
+  const future = dayjs().add(1, 'day')
+  expect(past.isSameOrAfter()).toBe(false, 'past is before now')
+  expect(future.isSameOrAfter()).toBe(true, 'future is not before now')
+})
+
 test('is same or after month', () => {
   const m = dayjs(new Date(2011, 2, 3, 4, 5, 6, 7))
   const mCopy = dayjs(m)
diff --git a/test/plugin/isSameOrBefore.test.js b/test/plugin/isSameOrBefore.test.js
index a8e1821..3ceff55 100644
--- a/test/plugin/isSameOrBefore.test.js
+++ b/test/plugin/isSameOrBefore.test.js
@@ -34,6 +34,13 @@ test('is same or before without units', () => {
   expect(+m).toEqual(+mCopy, 'isSameOrBefore second should not change moment')
 })
 
+test('is same or before without date', () => {
+  const past = dayjs().subtract(1, 'day')
+  const future = dayjs().add(1, 'day')
+  expect(past.isSameOrBefore()).toBe(true, 'past is before now')
+  expect(future.isSameOrBefore()).toBe(false, 'future is not before now')
+})
+
 test('is same or before year', () => {
   const m = dayjs(new Date(2011, 1, 2, 3, 4, 5, 6))
   const mCopy = dayjs(m)