Skip to content

Commit a8f38a2

Browse files
authored
Merge pull request #2694 from iamkun/dev
D2M
2 parents 10c9368 + 9dbc373 commit a8f38a2

File tree

9 files changed

+316
-22
lines changed

9 files changed

+316
-22
lines changed

README.md

+10-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ English | [简体中文](./docs/zh-cn/README.zh-CN.md) | [日本語](./docs/ja/R
55
alt="Day.js" /></a></p>
66
<p align="center">Fast <b>2kB</b> alternative to Moment.js with the same modern API</p>
77
<p align="center">
8-
<a href="https://unpkg.com/dayjs/dayjs.min.js"><img
8+
<a href="https://bundlephobia.com/package/dayjs"><img
99
src="https://img.shields.io/bundlephobia/minzip/dayjs?style=flat-square&color=%2345cc11"
1010
alt="Gzip Size"></a>
1111
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square&colorB=51C838"
@@ -115,8 +115,8 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
115115
<img width="70" src="https://user-images.githubusercontent.com/17680888/197092231-2367b5eb-1e43-467e-a311-23f7cd97b086.png">
116116
</a>
117117
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
118-
<a href="https://github.com/alan-eu" target="_blank">
119-
<img width="70" src="https://avatars.githubusercontent.com/u/18175329?s=52&v=4">
118+
<a href="https://github.com/ken-swyfft" target="_blank">
119+
<img width="70" src="https://avatars.githubusercontent.com/u/65305317?v=4">
120120
</a>
121121
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
122122
<a href="https://opencollective.com/sight-and-sound-ministries" target="_blank">
@@ -127,25 +127,22 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
127127
<img width="70" src="https://images.opencollective.com/chudovo/3c866f5/logo/256.png?height=256">
128128
</a>
129129
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
130-
<a href="https://github.com/ken-swyfft" target="_blank">
131-
<img width="70" src="https://avatars.githubusercontent.com/u/65305317?v=4">
132-
</a>
133-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
134-
<a href="https://github.com/storyblok" target="_blank">
135-
<img width="70" src="https://avatars.githubusercontent.com/u/13880908?s=200&v=4">
130+
<a href="https://github.com/alan-eu" target="_blank">
131+
<img width="70" src="https://avatars.githubusercontent.com/u/18175329?s=52&v=4">
136132
</a>
137133
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
138134
<a href="https://www.exoflare.com/open-source/?utm_source=dayjs&utm_campaign=open_source" target="_blank">
139135
<img width="70" src="https://user-images.githubusercontent.com/17680888/162761622-1407a849-0c41-4591-8aa9-f98114ec2092.png">
140136
</a>
141137
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
138+
<a href="https://github.com/storyblok" target="_blank">
139+
<img width="70" src="https://avatars.githubusercontent.com/u/13880908?s=200&v=4">
140+
</a>
141+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
142142
<a href="https://bestkru.com/" target="_blank">
143143
<img width="70" src="https://avatars.githubusercontent.com/u/159320286" alt="BestKru">
144144
</a>
145-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
146-
<a href="https://opencollective.com/anonstories" target="_blank"><img width="70" src="https://images.opencollective.com/anonstories/7e826c0/avatar/256.png"></a>
147-
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
148-
<a href="https://opencollective.com/datawrapper" target="_blank"><img width="70" src="https://images.opencollective.com/datawrapper/c13e229/logo.png"></a>
145+
149146

150147
## Contributors
151148

docs/demo/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dayjs from 'dayjs'
2+
// basic usage
3+
dayjs().format()
4+
5+
// parse
6+
dayjs('2018-08-08').format()
7+
8+
// format
9+
dayjs().format('YYYY-MM-DD')

docs/sw/README-sw.md

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
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)
2+
3+
<p align="center"><a href="https://day.js.org/" target="_blank" rel="noopener noreferrer"><img width="550"
4+
src="https://user-images.githubusercontent.com/17680888/39081119-3057bbe2-456e-11e8-862c-646133ad4b43.png"
5+
alt="Day.js" /></a></p>
6+
<p align="center">Fast <b>2kB</b> mbadala wa Moment.js ukiwa na API zinazofanana na za kisasa</p>
7+
<p align="center">
8+
<a href="https://bundlephobia.com/package/dayjs"><img
9+
src="https://img.shields.io/bundlephobia/minzip/dayjs?style=flat-square&color=%2345cc11"
10+
alt="Gzip Size"></a>
11+
<a href="https://www.npmjs.com/package/dayjs"><img src="https://img.shields.io/npm/v/dayjs.svg?style=flat-square&colorB=51C838"
12+
alt="NPM Version"></a>
13+
<a href="https://github.com/iamkun/dayjs/actions/workflows/check.yml"><img
14+
src="https://img.shields.io/github/actions/workflow/status/iamkun/dayjs/check.yml?style=flat-square" alt="Build Status"></a>
15+
<a href="https://codecov.io/gh/iamkun/dayjs"><img
16+
src="https://img.shields.io/codecov/c/github/iamkun/dayjs/master.svg?style=flat-square" alt="Codecov"></a>
17+
<a href="https://github.com/iamkun/dayjs/blob/master/LICENSE"><img
18+
src="https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square" alt="License"></a>
19+
<br>
20+
<a href="https://saucelabs.com/u/dayjs">
21+
<img width="750" src="https://user-images.githubusercontent.com/17680888/40040137-8e3323a6-584b-11e8-9dba-bbe577ee8a7b.png" alt="Sauce Test Status">
22+
</a>
23+
</p>
24+
25+
> 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.
26+
27+
```js
28+
dayjs().startOf('month').add(1, 'day').set('year', 2018).format('YYYY-MM-DD HH:mm:ss');
29+
```
30+
31+
* 🕒 Michoro na API maarufu za Moment.js
32+
* 💪 Hazibadiliki
33+
* 🔥 Zinaunganishwa
34+
* 🌐 Zinaruhusu I18n
35+
* 📦 Maktaba ndogo ya 2kb
36+
* 👫 Browser zote zinaruhusu
37+
38+
---
39+
40+
## Kuanza
41+
42+
### Nyaraka
43+
44+
Unaweza kutafuta maelekezo zaidi ya API na nyaraka zingine kupitia tovuti ya [day.js.org](https://day.js.org/).
45+
46+
### Kusakinisha
47+
48+
```console
49+
npm install dayjs --save
50+
```
51+
52+
📚[Maelekezo ya Kusakinisha](https://day.js.org/docs/en/installation/installation)
53+
54+
### API
55+
56+
Ni rahisi kutumia Day.js kupitisha, kuhakiki, kubadili na kuonesha tarehe na mda.
57+
58+
```javascript
59+
dayjs('2018-08-08') // changanua
60+
61+
dayjs().format('{YYYY} MM-DDTHH:mm:ss SSS [Z] A') // display
62+
63+
dayjs().set('month', 3).month() // chukua na weka
64+
dayjs().add(1, 'year') // manipulate
65+
66+
dayjs().isBefore(dayjs()) // query
67+
```
68+
69+
📚[Maelekezo Ya API](https://day.js.org/docs/en/parse/parse)
70+
71+
### I18n
72+
73+
Day.js ina ruhusu kwa internalization.
74+
75+
Lakini hakuna hata moja ambayo itawekwa katika utengezaji endapo utatumia.
76+
77+
```javascript
78+
import 'dayjs/locale/es' // load on demand
79+
80+
dayjs.locale('es') // use Spanish locale globally
81+
82+
dayjs('2018-05-05').locale('zh-cn').format() // use Chinese Simplified locale in a specific instance
83+
```
84+
85+
📚[Internationalization](https://day.js.org/docs/en/i18n/i18n)
86+
87+
### Zana
88+
89+
Zana ya Kujumuisha ni moduli inayojitegemea ambayo inaweza ikaweka katika Day.js ili kuweza kuongeza uwezo au kuongeza sifa.
90+
91+
```javascript
92+
import advancedFormat from 'dayjs/plugin/advancedFormat' // Upakiaji kwa Mahitaji
93+
94+
dayjs.extend(advancedFormat) // tumia plugin
95+
96+
dayjs().format('Q Do k kk X x') // njia zaidi zilizopo
97+
```
98+
99+
📚[Idadi Ya Plugin](https://day.js.org/docs/en/plugin/plugin)
100+
101+
### Trend Zinazoweza Tumika
102+
103+
<a href="https://npm-compare.com/moment,dayjs/#timeRange=THREE_YEARS" target="_blank">
104+
<img src="https://user-images.githubusercontent.com/3455798/270162667-c7bd2ebe-675e-45c6-a2c9-dc67f3b65d6e.png">
105+
</a>
106+
107+
## Wafadhili
108+
109+
Toa mchango wako kwa huu mradi kwa kuwa mfadhili. Nembo yako itaonekana hapa pamoja na link ya kwenda kwenye tovuti yako.
110+
111+
[[Kuwa mfadhili kupitia GitHub](https://github.com/sponsors/iamkun/)] [[Kuwa mfadhili kupitia OpenCollective](https://opencollective.com/dayjs#sponsor)]
112+
113+
<a href="https://toyokumo.co.jp" target="_blank">
114+
<img width="70" src="https://user-images.githubusercontent.com/17680888/197092231-2367b5eb-1e43-467e-a311-23f7cd97b086.png">
115+
</a>
116+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
117+
<a href="https://github.com/ken-swyfft" target="_blank">
118+
<img width="70" src="https://avatars.githubusercontent.com/u/65305317?v=4">
119+
</a>
120+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
121+
<a href="https://opencollective.com/sight-and-sound-ministries" target="_blank">
122+
<img width="70" src="https://user-images.githubusercontent.com/17680888/232316426-cb99b4cf-0ccb-4e73-a6ce-e16dba6aadf4.png">
123+
</a>
124+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
125+
<a href="https://chudovo.com/" target="_blank">
126+
<img width="70" src="https://images.opencollective.com/chudovo/3c866f5/logo/256.png?height=256">
127+
</a>
128+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
129+
<a href="https://github.com/alan-eu" target="_blank">
130+
<img width="70" src="https://avatars.githubusercontent.com/u/18175329?s=52&v=4">
131+
</a>
132+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
133+
<a href="https://www.exoflare.com/open-source/?utm_source=dayjs&utm_campaign=open_source" target="_blank">
134+
<img width="70" src="https://user-images.githubusercontent.com/17680888/162761622-1407a849-0c41-4591-8aa9-f98114ec2092.png">
135+
</a>
136+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
137+
<a href="https://github.com/storyblok" target="_blank">
138+
<img width="70" src="https://avatars.githubusercontent.com/u/13880908?s=200&v=4">
139+
</a>
140+
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
141+
<a href="https://bestkru.com/" target="_blank">
142+
<img width="70" src="https://avatars.githubusercontent.com/u/159320286" alt="BestKru">
143+
</a>
144+
145+
146+
## Wachangiaji
147+
148+
Huu mradi umefika hapa ulipo hapa shukrani ziende kwa watu wote wanao changia.
149+
150+
Tafadhali tupe 💖 nyota/maua 💖 kutuunga mkono sisi.Ahsante.
151+
152+
Na ahsante kwa wafadhili wote! 🙏
153+
154+
<a href="https://opencollective.com/dayjs/backer/0/website?requireActive=false" target="_blank"><img width="35" src="https://opencollective.com/dayjs/backer/0/avatar.svg?requireActive=false"></a>
155+
<a href="https://opencollective.com/dayjs/backer/1/website?requireActive=false" target="_blank"><img width="35" src="https://opencollective.com/dayjs/backer/1/avatar.svg?requireActive=false"></a>
156+
<a href="https://opencollective.com/dayjs/backer/2/website?requireActive=false" target="_blank"><img width="35" src="https://opencollective.com/dayjs/backer/2/avatar.svg?requireActive=false"></a>
157+
<a href="https://opencollective.com/dayjs/backer/3/website?requireActive=false" target="_blank"><img width="35" src="https://opencollective.com/dayjs/backer/3/avatar.svg?requireActive=false"></a>
158+
<br />
159+
<a href="https://opencollective.com/dayjs#backers" target="_blank"><img src="https://opencollective.com/dayjs/contributors.svg?width=890" /></a>
160+
161+
## Leseni
162+
163+
Day.js ipo chini ya kibali cha [leseni ya MTI](./LICENSE).

src/plugin/negativeYear/index.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export default (_, c, dayjs) => {
2+
const proto = c.prototype
3+
4+
const parseDate = (cfg) => {
5+
const { date, utc } = cfg
6+
if (typeof date === 'string' && date.charAt(0) === '-') {
7+
const normalData = date.slice(1)
8+
let newDate = dayjs(normalData)
9+
if (utc) {
10+
newDate = dayjs.utc(normalData)
11+
} else {
12+
newDate = dayjs(normalData)
13+
}
14+
const fullYear = newDate.year()
15+
if (date.indexOf(`-${fullYear}`) !== -1) {
16+
return dayjs(newDate).subtract(fullYear * 2, 'year').toDate()
17+
}
18+
return date
19+
}
20+
return date
21+
}
22+
23+
const oldParse = proto.parse
24+
proto.parse = function (cfg) {
25+
cfg.date = parseDate.bind(this)(cfg)
26+
oldParse.bind(this)(cfg)
27+
}
28+
}
29+

src/plugin/timezone/index.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,18 @@ export default (o, c, d) => {
9797
const date = this.toDate()
9898
const target = date.toLocaleString('en-US', { timeZone: timezone })
9999
const diff = Math.round((date - new Date(target)) / 1000 / 60)
100-
let ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
101-
.utcOffset((-Math.round(date.getTimezoneOffset() / 15) * 15) - diff, true)
102-
if (keepLocalTime) {
103-
const newOffset = ins.utcOffset()
104-
ins = ins.add(oldOffset - newOffset, MIN)
100+
const offset = (-Math.round(date.getTimezoneOffset() / 15) * 15) - diff
101+
const isUTC = !Number(offset)
102+
let ins
103+
if (isUTC) { // if utcOffset is 0, turn it to UTC mode
104+
ins = this.utcOffset(0, keepLocalTime)
105+
} else {
106+
ins = d(target, { locale: this.$L }).$set(MS, this.$ms)
107+
.utcOffset(offset, true)
108+
if (keepLocalTime) {
109+
const newOffset = ins.utcOffset()
110+
ins = ins.add(oldOffset - newOffset, MIN)
111+
}
105112
}
106113
ins.$x.$timezone = timezone
107114
return ins

test/plugin/negativeYear.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import MockDate from 'mockdate'
2+
import dayjs from 'dayjs'
3+
import negativeYear from '../../src/plugin/negativeYear'
4+
import utc from '../../src/plugin/utc'
5+
import { REGEX_PARSE } from '../../src/constant'
6+
7+
8+
dayjs.extend(negativeYear)
9+
dayjs.extend(utc)
10+
11+
beforeEach(() => {
12+
MockDate.set(new Date())
13+
})
14+
15+
afterEach(() => {
16+
MockDate.reset()
17+
})
18+
19+
describe('negativeYear', () => {
20+
it('parses negative years', () => {
21+
expect(dayjs('-2020-01-01').year()).toBe(-2020)
22+
const date = '-2021/01/03'
23+
const date2 = '01/03/-2021'
24+
const date3 = '01-03--2021'
25+
const d = date.match(REGEX_PARSE)
26+
expect(dayjs(date).format('YYYY-MM-DD')).toBe('-2021-01-03')
27+
expect(dayjs(date2).format('YYYY-MM-DD')).toBe('Invalid Date')
28+
expect(dayjs(date3).format()).toBe('Invalid Date')
29+
expect(d).toBe(null)
30+
})
31+
32+
it('does not parse non-negative years', () => {
33+
expect(dayjs('2020-01-01').year()).toBe(2020)
34+
})
35+
36+
it('works with other plugins', () => {
37+
expect(dayjs.utc('-2020-01-01').year()).toBe(-2020)
38+
})
39+
40+
it('Add and subtract with negative years', () => {
41+
expect(dayjs('-2006').add(1, 'y')).toEqual(dayjs('-2005'))
42+
expect(dayjs('-2006').subtract(1, 'y')).toEqual(dayjs('-2007'))
43+
expect(dayjs('-2006').add(1, 'y').format('YYYY')).toBe(dayjs('-2005').format('YYYY'))
44+
expect(dayjs('-2006').subtract(1, 'y').format('YYYY')).toBe(dayjs('-2007').format('YYYY'))
45+
})
46+
47+
it('Compare date with negative years', () => {
48+
expect(dayjs('-2006').isAfter(dayjs('-2007'))).toBeTruthy()
49+
expect(dayjs('-2006').isBefore(dayjs('-2005'))).toBeTruthy()
50+
expect(dayjs('-2006').isSame('-2006')).toBeTruthy()
51+
})
52+
})

test/plugin/timezone.test.js

+22
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,25 @@ describe('startOf and endOf', () => {
330330
expect(tzWithLocality.startOf('week').format('YYYY-MM-DD')).toEqual('2023-02-15')
331331
})
332332
})
333+
334+
335+
describe('UTC timezone', () => {
336+
it('TZ with UTC with Locale', () => {
337+
const test1 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Seoul').locale('en')
338+
expect(test1.hour()).toBe(9)
339+
const test2 = dayjs('2000-01-01T09:00:00+09:00').tz('Asia/Hong_Kong').locale('en')
340+
expect(test2.hour()).toBe(8)
341+
const test3 = dayjs('2000-01-01T09:00:00+09:00').tz('Etc/UTC').locale('en')
342+
expect(test3.hour()).toBe(0)
343+
})
344+
345+
it('TZ with UTC', () => {
346+
const dayjs1 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false)
347+
expect(dayjs1.format()).toBe('2000-01-01T00:01:00Z')
348+
const moment1 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', false)
349+
expect(moment1.format()).toBe('2000-01-01T00:01:00Z')
350+
const dayjs2 = dayjs('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true)
351+
const moment2 = moment('2000-01-01T09:01:00+09:00').tz('Etc/UTC', true)
352+
expect(dayjs2.format()).toBe(moment2.format())
353+
})
354+
})

types/plugin/minMax.d.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@ declare const plugin: PluginFunc
44
export = plugin
55

66
declare module 'dayjs' {
7-
export function max(dayjs: Dayjs[]): Dayjs | null
8-
export function max(...dayjs: Dayjs[]): Dayjs | null
9-
export function min(dayjs: Dayjs[]): Dayjs | null
10-
export function min(...dayjs: Dayjs[]): Dayjs | null
7+
export function max(dayjs: [Dayjs, ...Dayjs[]]): Dayjs
8+
export function max(noDates: never[]): null
9+
export function max(maybeDates: Dayjs[]): Dayjs | null
10+
11+
export function max(...dayjs: [Dayjs, ...Dayjs[]]): Dayjs
12+
export function max(...noDates: never[]): null
13+
export function max(...maybeDates: Dayjs[]): Dayjs | null
14+
15+
export function min(dayjs: [Dayjs, ...Dayjs[]]): Dayjs
16+
export function min(noDates: never[]): null
17+
export function min(maybeDates: Dayjs[]): Dayjs | null
18+
19+
export function min(...dayjs: [Dayjs, ...Dayjs[]]): Dayjs
20+
export function min(...noDates: never[]): null
21+
export function min(...maybeDates: Dayjs[]): Dayjs | null
1122
}

types/plugin/negativeYear.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {PluginFunc} from 'dayjs'
2+
3+
declare const plugin: PluginFunc
4+
export = plugin

0 commit comments

Comments
 (0)