File tree 3 files changed +41
-0
lines changed
3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,17 @@ List of added formats:
144
144
| ` BBBB ` | 2561 | Full BE Year (Year + 543) |
145
145
| ` BB ` | 61 | 2-digit of BE Year |
146
146
147
+ ### WeekOfYear
148
+ - WeekOfYear adds ` .week() ` API to returns a ` number ` indicating the ` Dayjs ` 's week of the year.
149
+
150
+ ``` javascript
151
+ import weekOfYear from ' dayjs/plugin/weekOfYear'
152
+
153
+ dayjs .extend (weekOfYear)
154
+
155
+ dayjs (' 06/27/2018' ).week () // 26
156
+ ```
157
+
147
158
## Customize
148
159
149
160
You could build your own Day.js plugin to meet different needs.
Original file line number Diff line number Diff line change
1
+ import { MILLISECONDS_A_DAY } from '../../constant'
2
+
3
+ export default ( o , c ) => {
4
+ const proto = c . prototype
5
+ proto . week = function ( ) {
6
+ const day = this . $W || 7 // Return sunday as 7
7
+ // Create date at nearest thursday
8
+ const ins = new Date ( this . $y , this . $M , ( this . $D - day ) + 4 )
9
+ const yearStart = new Date ( Date . UTC ( this . $y , 0 , 1 ) ) // Get first day of year
10
+ return Math . ceil ( ( ( ( ins - yearStart ) / MILLISECONDS_A_DAY ) + 1 ) / 7 ) // Calculate weeks
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ import moment from 'moment'
2
+ import MockDate from 'mockdate'
3
+ import dayjs from '../../src'
4
+ import weekOfYear from '../../src/plugin/weekOfYear'
5
+
6
+ dayjs . extend ( weekOfYear )
7
+
8
+ beforeEach ( ( ) => {
9
+ MockDate . set ( new Date ( ) )
10
+ } )
11
+
12
+ afterEach ( ( ) => {
13
+ MockDate . reset ( )
14
+ } )
15
+
16
+ it ( 'Week of year' , ( ) => {
17
+ expect ( dayjs ( ) . week ( ) ) . toBe ( moment ( ) . week ( ) )
18
+ } )
You can’t perform that action at this time.
0 commit comments