From 6a42e0d7398639238f575d51287daaf4d495a2a3 Mon Sep 17 00:00:00 2001 From: klm Date: Sun, 12 May 2024 13:01:58 +0000 Subject: [PATCH 01/12] fix: Add NegativeYear Plugin support (#2640) --- src/plugin/negativeYear/index.js | 23 ++++++++++++++ test/plugin/negativeYear.test.js | 52 ++++++++++++++++++++++++++++++++ types/plugin/negativeYear.d.ts | 4 +++ 3 files changed, 79 insertions(+) create mode 100644 src/plugin/negativeYear/index.js create mode 100644 test/plugin/negativeYear.test.js create mode 100644 types/plugin/negativeYear.d.ts diff --git a/src/plugin/negativeYear/index.js b/src/plugin/negativeYear/index.js new file mode 100644 index 000000000..6a3fbea7a --- /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/test/plugin/negativeYear.test.js b/test/plugin/negativeYear.test.js new file mode 100644 index 000000000..a9713fa26 --- /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() + }) +}) diff --git a/types/plugin/negativeYear.d.ts b/types/plugin/negativeYear.d.ts new file mode 100644 index 000000000..43bb86878 --- /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 From e5a1c038a2c43f699e77189b55bb0052157b65ab Mon Sep 17 00:00:00 2001 From: iamkun Date: Sat, 18 May 2024 01:03:44 +0800 Subject: [PATCH 02/12] chore: Update README.md --- README.md | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b50a700cb..6ded43982 100644 --- a/README.md +++ b/README.md @@ -115,37 +115,26 @@ Support this project by becoming a sponsor. Your logo will show up here with a l          - - + +                   - - - -         - - + +                   - - - -         BestKru -         - -         - + ## Contributors From 1e61e425652acd57f671856b8e7e25216e75c437 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 20 May 2024 19:51:37 +0800 Subject: [PATCH 03/12] chore: update doc demo --- docs/demo/index.js | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 docs/demo/index.js diff --git a/docs/demo/index.js b/docs/demo/index.js new file mode 100644 index 000000000..0e4a87dba --- /dev/null +++ b/docs/demo/index.js @@ -0,0 +1,3 @@ +import dayjs from 'dayjs' +// basic usage +dayjs().format() From 4fbe94aaba8c815a42cf4d23dabac918ec50e68c Mon Sep 17 00:00:00 2001 From: Ben Saufley Date: Mon, 20 May 2024 08:11:56 -0400 Subject: [PATCH 04/12] fix: Improve typing for min/max plugin (#2573) No null if at least one date is passed; only null if explicitly no dates are passed. Co-authored-by: iamkun --- types/plugin/minMax.d.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/types/plugin/minMax.d.ts b/types/plugin/minMax.d.ts index 4c5f6dcd7..7d0827f13 100644 --- a/types/plugin/minMax.d.ts +++ b/types/plugin/minMax.d.ts @@ -4,8 +4,19 @@ declare const plugin: PluginFunc export = plugin declare module 'dayjs' { - export function max(dayjs: Dayjs[]): Dayjs | null - export function max(...dayjs: Dayjs[]): Dayjs | null - export function min(dayjs: Dayjs[]): Dayjs | null - export function min(...dayjs: Dayjs[]): Dayjs | null + export function max(dayjs: [Dayjs, ...Dayjs[]]): Dayjs + export function max(noDates: never[]): null + export function max(maybeDates: Dayjs[]): Dayjs | null + + export function max(...dayjs: [Dayjs, ...Dayjs[]]): Dayjs + export function max(...noDates: never[]): null + export function max(...maybeDates: Dayjs[]): Dayjs | null + + export function min(dayjs: [Dayjs, ...Dayjs[]]): Dayjs + export function min(noDates: never[]): null + export function min(maybeDates: Dayjs[]): Dayjs | null + + export function min(...dayjs: [Dayjs, ...Dayjs[]]): Dayjs + export function min(...noDates: never[]): null + export function min(...maybeDates: Dayjs[]): Dayjs | null } From f4cb2cfd77246116cb4151c74ca2f600a17cd951 Mon Sep 17 00:00:00 2001 From: iamkun Date: Wed, 22 May 2024 22:05:06 +0800 Subject: [PATCH 05/12] chore: Update README.md --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6ded43982..74d88057e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/R alt="Day.js" />

Fast 2kB alternative to Moment.js with the same modern API

- Gzip Size          + + + +         + + + +         From 8d5d521602a2ee80a5ae13cb916b8d0e6bb287a6 Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 21 Jun 2024 23:43:22 +0800 Subject: [PATCH 06/12] chore: update demo --- docs/demo/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/demo/index.js b/docs/demo/index.js index 0e4a87dba..90e501596 100644 --- a/docs/demo/index.js +++ b/docs/demo/index.js @@ -1,3 +1,6 @@ import dayjs from 'dayjs' // basic usage dayjs().format() + +// parse +dayjs('2018-08-08').format() From 9635a93aa32bdcabf79536e41bd300b83ec1c12c Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 18 Jul 2024 12:05:08 +0800 Subject: [PATCH 07/12] chore: update demo --- docs/demo/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/demo/index.js b/docs/demo/index.js index 90e501596..4299e3013 100644 --- a/docs/demo/index.js +++ b/docs/demo/index.js @@ -4,3 +4,6 @@ dayjs().format() // parse dayjs('2018-08-08').format() + +// format +dayjs().format('YYYY-MM-DD') From d0e6738a66e1b65d3706aad2f9168ebb43d4f887 Mon Sep 17 00:00:00 2001 From: Yungoo Kong Date: Thu, 18 Jul 2024 20:20:41 +0900 Subject: [PATCH 08/12] fix: Fix zero offset issue when use tz with locale (#2532) * test: add test case for tz with locale * fix: set utc as false if $offset is 0 --- src/index.js | 2 +- test/timezone.test.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 061ade178..f28d64984 100644 --- a/src/index.js +++ b/src/index.js @@ -50,7 +50,7 @@ const dayjs = function (date, c) { const wrapper = (date, instance) => dayjs(date, { locale: instance.$L, - utc: instance.$u, + utc: instance.$offset !== 0 && instance.$u, x: instance.$x, $offset: instance.$offset // todo: refactor; do not use this.$offset in you code }) diff --git a/test/timezone.test.js b/test/timezone.test.js index 42cab8934..9d61fe060 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -3,6 +3,7 @@ import moment from 'moment' import dayjs from '../src' import timezone from '../src/plugin/timezone' import utc from '../src/plugin/utc' +import '../src/locale/en' dayjs.extend(utc) dayjs.extend(timezone) @@ -80,3 +81,12 @@ it('UTC diff in DST', () => { expect(day1.diff(day2, 'd')) .toBe(-3) }) + +it('TZ with Locale', () => { + const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en') + expect(test1.hour()).toBe(9) + const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en') + expect(test2.hour()).toBe(8) + const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en') + expect(test3.hour()).toBe(0) +}) From f3ef705613af83333fe132b470896a65e12f31b0 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 18 Jul 2024 19:37:49 +0800 Subject: [PATCH 09/12] 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() } From b575c81a8c9c85c7a0baf6f608a12f9d3ba95bd1 Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 18 Jul 2024 20:30:10 +0800 Subject: [PATCH 10/12] fix: timezone plugin currect parse UTC tz (#2693) --- src/index.js | 2 +- src/plugin/timezone/index.js | 17 ++++++++++++----- test/plugin/timezone.test.js | 22 ++++++++++++++++++++++ test/timezone.test.js | 9 --------- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index f28d64984..061ade178 100644 --- a/src/index.js +++ b/src/index.js @@ -50,7 +50,7 @@ const dayjs = function (date, c) { const wrapper = (date, instance) => dayjs(date, { locale: instance.$L, - utc: instance.$offset !== 0 && instance.$u, + utc: instance.$u, x: instance.$x, $offset: instance.$offset // todo: refactor; do not use this.$offset in you code }) diff --git a/src/plugin/timezone/index.js b/src/plugin/timezone/index.js index 832fb4a73..bcacf9ab6 100644 --- a/src/plugin/timezone/index.js +++ b/src/plugin/timezone/index.js @@ -97,11 +97,18 @@ export default (o, c, d) => { const date = this.toDate() const target = date.toLocaleString('en-US', { timeZone: timezone }) const diff = Math.round((date - new Date(target)) / 1000 / 60) - let ins = d(target, { locale: this.$L }).$set(MS, this.$ms) - .utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true) - if (keepLocalTime) { - const newOffset = ins.utcOffset() - ins = ins.add(oldOffset - newOffset, MIN) + const offset = (-Math.round(date.getTimezoneOffset() / 15) * 15) - diff + const isUTC = !Number(offset) + let ins + if (isUTC) { // if utcOffset is 0, turn it to UTC mode + ins = this.utcOffset(0, keepLocalTime) + } else { + ins = d(target, { locale: this.$L }).$set(MS, this.$ms) + .utcOffset(offset, true) + if (keepLocalTime) { + const newOffset = ins.utcOffset() + ins = ins.add(oldOffset - newOffset, MIN) + } } ins.$x.$timezone = timezone return ins diff --git a/test/plugin/timezone.test.js b/test/plugin/timezone.test.js index c4529c6c8..d83a03f8a 100644 --- a/test/plugin/timezone.test.js +++ b/test/plugin/timezone.test.js @@ -330,3 +330,25 @@ describe('startOf and endOf', () => { expect(tzWithLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-15') }) }) + + +describe('UTC timezone', () => { + it('TZ with UTC with Locale', () => { + const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en') + expect(test1.hour()).toBe(9) + const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en') + expect(test2.hour()).toBe(8) + const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en') + expect(test3.hour()).toBe(0) + }) + + it('TZ with UTC', () => { + const dayjs1 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false) + expect(dayjs1.format()).toBe('2000-01-01T00:01:00Z') + const moment1 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false) + expect(moment1.format()).toBe('2000-01-01T00:01:00Z') + const dayjs2 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true) + const moment2 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true) + expect(dayjs2.format()).toBe(moment2.format()) + }) +}) diff --git a/test/timezone.test.js b/test/timezone.test.js index 9d61fe060..aea31fda7 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -81,12 +81,3 @@ it('UTC diff in DST', () => { expect(day1.diff(day2, 'd')) .toBe(-3) }) - -it('TZ with Locale', () => { - const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en') - expect(test1.hour()).toBe(9) - const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en') - expect(test2.hour()).toBe(8) - const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en') - expect(test3.hour()).toBe(0) -}) From 5e6d8b06cf0f5702442c20c6236cc6919cd04098 Mon Sep 17 00:00:00 2001 From: Godbless Nyagawa <110627172+Njoxpy@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:30:55 +0300 Subject: [PATCH 11/12] chore: added README translation for Swahili speakers (#2691) --- docs/sw/README-sw.md | 163 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 docs/sw/README-sw.md diff --git a/docs/sw/README-sw.md b/docs/sw/README-sw.md new file mode 100644 index 000000000..df1d173a6 --- /dev/null +++ b/docs/sw/README-sw.md @@ -0,0 +1,163 @@ +Swahili | [English](../../README.md) | [Kireno](../pt-br/README-pt-br.md) | [Kichina](../zh-cn/README.zh-CN.md) | [Kijapani](../ja/README-ja.md) | [Kikorea](../ko/README-ko.md) | [Kihispania](../es-es/README-es-es.md) | [Kirusi](../ru/README-ru.md) | [Kituruki](../tr/README-tr.md) | [Sinhala](../si/README-si.md) | [Kiebrania](../he/README-he.md) + +

Day.js

+

Fast 2kB mbadala wa Moment.js ukiwa na API zinazofanana na za kisasa

+

+ Gzip Size + NPM Version + Build Status + Codecov + License +
+ + Sauce Test Status + +

+ +> Day.js ni Maktaba ya JavaScript ya minimalist ambayo inachanganua, kuthibitisha, kudhibiti, na kuonyesha tarehe na nyakati kwa vivinjari vya kisasa na API inayofanana sana na Moment.js. Ikiwa unatumia Moment.js, tayari unajua jinsi ya kutumia Day.js. + +```js +dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss'); +``` + +* 🕒 Michoro na API maarufu za Moment.js +* 💪 Hazibadiliki +* 🔥 Zinaunganishwa +* 🌐 Zinaruhusu I18n +* 📦 Maktaba ndogo ya 2kb +* 👫 Browser zote zinaruhusu + +--- + +## Kuanza + +### Nyaraka + +Unaweza kutafuta maelekezo zaidi ya API na nyaraka zingine kupitia tovuti ya [day.js.org](https://day.js.org/). + +### Kusakinisha + +```console +npm install dayjs --save +``` + +📚[Maelekezo ya Kusakinisha](https://day.js.org/docs/en/installation/installation) + +### API + +Ni rahisi kutumia Day.js kupitisha, kuhakiki, kubadili na kuonesha tarehe na mda. + +```javascript +dayjs('2018-08-08') // changanua + +dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // display + +dayjs().set('month', 3).month() // chukua na weka +dayjs().add(1, 'year') // manipulate + +dayjs().isBefore(dayjs()) // query +``` + +📚[Maelekezo Ya API](https://day.js.org/docs/en/parse/parse) + +### I18n + +Day.js ina ruhusu kwa internalization. + +Lakini hakuna hata moja ambayo itawekwa katika utengezaji endapo utatumia. + +```javascript +import 'dayjs/locale/es' // load on demand + +dayjs.locale('es') // use Spanish locale globally + +dayjs('2018-05-05').locale('zh-cn').format() // use Chinese Simplified locale in a specific instance +``` + +📚[Internationalization](https://day.js.org/docs/en/i18n/i18n) + +### Zana + +Zana ya Kujumuisha ni moduli inayojitegemea ambayo inaweza ikaweka katika Day.js ili kuweza kuongeza uwezo au kuongeza sifa. + +```javascript +import advancedFormat from 'dayjs/plugin/advancedFormat' // Upakiaji kwa Mahitaji + +dayjs.extend(advancedFormat) // tumia plugin + +dayjs().format('Q Do k kk X x') // njia zaidi zilizopo +``` + +📚[Idadi Ya Plugin](https://day.js.org/docs/en/plugin/plugin) + +### Trend Zinazoweza Tumika + + + + + +## Wafadhili + +Toa mchango wako kwa huu mradi kwa kuwa mfadhili. Nembo yako itaonekana hapa pamoja na link ya kwenda kwenye tovuti yako. + +[[Kuwa mfadhili kupitia GitHub](https://github.com/sponsors/iamkun/)] [[Kuwa mfadhili kupitia OpenCollective](https://opencollective.com/dayjs#sponsor)] + + + + +         + + + +         + + + +         + + + +         + + + +         + + + +         + + + +         + + BestKru + + + +## Wachangiaji + +Huu mradi umefika hapa ulipo hapa shukrani ziende kwa watu wote wanao changia. + +Tafadhali tupe 💖 nyota/maua 💖 kutuunga mkono sisi.Ahsante. + +Na ahsante kwa wafadhili wote! 🙏 + + + + + +
+ + +## Leseni + +Day.js ipo chini ya kibali cha [leseni ya MTI](./LICENSE). \ No newline at end of file From 7b99f68c172c019752e5a3eeb88f401cd035a1ae Mon Sep 17 00:00:00 2001 From: iamkun Date: Thu, 18 Jul 2024 20:34:45 +0800 Subject: [PATCH 12/12] chore: rm useless import --- test/timezone.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/timezone.test.js b/test/timezone.test.js index aea31fda7..42cab8934 100644 --- a/test/timezone.test.js +++ b/test/timezone.test.js @@ -3,7 +3,6 @@ import moment from 'moment' import dayjs from '../src' import timezone from '../src/plugin/timezone' import utc from '../src/plugin/utc' -import '../src/locale/en' dayjs.extend(utc) dayjs.extend(timezone)