1
- export const convertToLocalDate = ( d , locale , options = { } ) => d . toLocaleDateString ( locale , options )
1
+ export const convertIsoWeekToDate = isoWeek => {
2
+ const [ year , week ] = isoWeek . split ( / w / i)
3
+ // Get date for 4th of January for year
4
+ const date = new Date ( Number ( year ) , 0 , 4 )
5
+ // Get previous Monday, add 7 days for each week after first
6
+ date . setDate (
7
+ // eslint-disable-next-line no-mixed-operators
8
+ date . getDate ( ) - ( date . getDay ( ) || 7 ) + 1 + ( Number ( week ) - 1 ) * 7
9
+ )
10
+ return date
11
+ }
12
+
13
+ export const convertToDateObject = ( date , selectionType ) => {
14
+ if ( date instanceof Date ) {
15
+ return date
16
+ }
17
+
18
+ if ( selectionType === 'week' ) {
19
+ return convertIsoWeekToDate ( date )
20
+ }
2
21
3
- export const convertToLocalTime = ( d , locale , options = { } ) => d . toLocaleTimeString ( locale , options )
22
+ return new Date ( date )
23
+ }
24
+
25
+ export const convertToLocalDate = ( d , locale , options = { } ) =>
26
+ d . toLocaleDateString ( locale , options )
27
+
28
+ export const convertToLocalTime = ( d , locale , options = { } ) =>
29
+ d . toLocaleTimeString ( locale , options )
4
30
5
31
export const createGroupsInArray = ( arr , numberOfGroups ) => {
6
32
const perGroup = Math . ceil ( arr . length / numberOfGroups )
7
- // eslint-disable-next-line unicorn/no-new-array
8
- return new Array ( numberOfGroups )
9
- . fill ( '' )
10
- . map ( ( _ , i ) => arr . slice ( i * perGroup , ( i + 1 ) * perGroup ) )
33
+ return Array . from ( { length : numberOfGroups } )
34
+ . fill ( '' )
35
+ . map ( ( _ , i ) => arr . slice ( i * perGroup , ( i + 1 ) * perGroup ) )
11
36
}
12
37
13
38
export const getCurrentYear = ( ) => new Date ( ) . getFullYear ( )
14
39
15
40
export const getCurrentMonth = ( ) => new Date ( ) . getMonth ( )
16
41
17
- export const getLocalDateFromString = ( string , locale , time ) => {
18
- const date = new Date ( 2013 , 11 , 31 , 17 , 19 , 22 )
19
- let regex = time ? date . toLocaleString ( locale ) : date . toLocaleDateString ( locale )
20
- regex = regex
21
- . replace ( '2013' , '(?<year>[0-9]{2,4})' )
22
- . replace ( '12' , '(?<month>[0-9]{1,2})' )
23
- . replace ( '31' , '(?<day>[0-9]{1,2})' )
24
- if ( time ) {
25
- regex = regex
26
- . replace ( '5' , '(?<hour>[0-9]{1,2})' )
27
- . replace ( '17' , '(?<hour>[0-9]{1,2})' )
28
- . replace ( '19' , '(?<minute>[0-9]{1,2})' )
29
- . replace ( '22' , '(?<second>[0-9]{1,2})' )
30
- . replace ( 'PM' , '(?<ampm>[A-Z]{2})' )
42
+ export const getDateBySelectionType = ( date , selectionType ) => {
43
+ if ( date === null ) {
44
+ return null
31
45
}
32
46
33
- const rgx = new RegExp ( `${ regex } ` )
34
- const partials = string . match ( rgx )
35
- if ( partials === null ) {
36
- return
47
+ if ( selectionType === 'week' ) {
48
+ return `${ date . getFullYear ( ) } W${ getWeekNumber ( date ) } `
37
49
}
38
50
39
- const newDate = partials . groups &&
40
- ( time ?
41
- new Date ( Number ( partials . groups . year , 10 ) , Number ( partials . groups . month , 10 ) - 1 , Number ( partials . groups . day ) , partials . groups . ampm ?
42
- ( partials . groups . ampm === 'PM' ?
43
- Number ( partials . groups . hour ) + 12 :
44
- Number ( partials . groups . hour ) ) :
45
- Number ( partials . groups . hour ) , Number ( partials . groups . minute ) , Number ( partials . groups . second ) ) :
46
- new Date ( Number ( partials . groups . year ) , Number ( partials . groups . month ) - 1 , Number ( partials . groups . day ) ) )
47
- return newDate
51
+ if ( selectionType === 'month' ) {
52
+ const monthNumber = `0${ date . getMonth ( ) + 1 } ` . slice ( - 2 )
53
+ return `${ date . getFullYear ( ) } -${ monthNumber } `
54
+ }
55
+
56
+ if ( selectionType === 'year' ) {
57
+ return `${ date . getFullYear ( ) } `
58
+ }
59
+
60
+ return date
48
61
}
49
62
50
63
export const getMonthName = ( month , locale ) => {
@@ -58,6 +71,7 @@ export const getMonthsNames = locale => {
58
71
const months = [ ]
59
72
const d = new Date ( )
60
73
d . setDate ( 1 )
74
+
61
75
for ( let i = 0 ; i < 12 ; i ++ ) {
62
76
d . setMonth ( i )
63
77
months . push ( d . toLocaleString ( locale , { month : 'short' } ) )
@@ -84,6 +98,7 @@ const getLeadingDays = (year, month, firstDayOfWeek) => {
84
98
const m = d . getMonth ( )
85
99
const firstWeekday = new Date ( y , m , 1 ) . getDay ( )
86
100
let leadingDays = 6 - ( 6 - firstWeekday ) - firstDayOfWeek
101
+
87
102
if ( firstDayOfWeek ) {
88
103
leadingDays = leadingDays < 0 ? 7 + leadingDays : leadingDays
89
104
}
@@ -125,16 +140,67 @@ const getTrailingDays = (year, month, leadingDays, monthDays) => {
125
140
}
126
141
127
142
export const getDayNumber = date =>
128
- Math . ceil ( ( date - new Date ( date . getFullYear ( ) , 0 , 0 ) ) / 1000 / 60 / 60 / 24 )
143
+ Math . ceil ( ( Number ( date ) - Number ( new Date ( date . getFullYear ( ) , 0 , 0 ) ) ) / 1000 / 60 / 60 / 24 )
129
144
130
- export const getWeekNumber = date => Math . ceil ( getDayNumber ( date ) / 7 )
145
+ export const getLocalDateFromString = ( string , locale , time ) => {
146
+ const date = new Date ( 2013 , 11 , 31 , 17 , 19 , 22 )
147
+ let regex = time ? date . toLocaleString ( locale ) : date . toLocaleDateString ( locale )
148
+ regex = regex
149
+ . replace ( '2013' , '(?<year>[0-9]{2,4})' )
150
+ . replace ( '12' , '(?<month>[0-9]{1,2})' )
151
+ . replace ( '31' , '(?<day>[0-9]{1,2})' )
152
+ if ( time ) {
153
+ regex = regex
154
+ . replace ( '5' , '(?<hour>[0-9]{1,2})' )
155
+ . replace ( '17' , '(?<hour>[0-9]{1,2})' )
156
+ . replace ( '19' , '(?<minute>[0-9]{1,2})' )
157
+ . replace ( '22' , '(?<second>[0-9]{1,2})' )
158
+ . replace ( 'PM' , '(?<ampm>[A-Z]{2})' )
159
+ }
160
+
161
+ const rgx = new RegExp ( `${ regex } ` )
162
+ const partials = string . match ( rgx )
163
+ if ( partials === null ) {
164
+ return
165
+ }
166
+
167
+ const newDate = partials . groups &&
168
+ ( time ?
169
+ new Date ( Number ( partials . groups . year , 10 ) , Number ( partials . groups . month , 10 ) - 1 , Number ( partials . groups . day ) , partials . groups . ampm ?
170
+ ( partials . groups . ampm === 'PM' ?
171
+ Number ( partials . groups . hour ) + 12 :
172
+ Number ( partials . groups . hour ) ) :
173
+ Number ( partials . groups . hour ) , Number ( partials . groups . minute ) , Number ( partials . groups . second ) ) :
174
+ new Date ( Number ( partials . groups . year ) , Number ( partials . groups . month ) - 1 , Number ( partials . groups . day ) ) )
175
+ return newDate
176
+ }
177
+
178
+ export const getWeekNumber = date => {
179
+ const week1 = new Date ( date . getFullYear ( ) , 0 , 4 )
180
+ return (
181
+ 1 +
182
+ Math . round (
183
+ // eslint-disable-next-line no-mixed-operators
184
+ ( ( date . getTime ( ) - week1 . getTime ( ) ) / 86400000 -
185
+ 3 +
186
+ ( ( week1 . getDay ( ) + 6 ) % 7 ) ) /
187
+ 7
188
+ )
189
+ )
190
+ }
131
191
132
192
export const getMonthDetails = ( year , month , firstDayOfWeek ) => {
133
193
const daysPrevMonth = getLeadingDays ( year , month , firstDayOfWeek )
134
194
const daysThisMonth = getMonthDays ( year , month )
135
- const daysNextMonth = getTrailingDays ( year , month , daysPrevMonth , daysThisMonth )
195
+ const daysNextMonth = getTrailingDays (
196
+ year ,
197
+ month ,
198
+ daysPrevMonth ,
199
+ daysThisMonth
200
+ )
136
201
const days = [ ...daysPrevMonth , ...daysThisMonth , ...daysNextMonth ]
137
202
const weeks = [ ]
203
+
138
204
for ( const [ index , day ] of days . entries ( ) ) {
139
205
if ( index % 7 === 0 || weeks . length === 0 ) {
140
206
weeks . push ( {
@@ -143,7 +209,7 @@ export const getMonthDetails = (year, month, firstDayOfWeek) => {
143
209
}
144
210
145
211
if ( ( index + 1 ) % 7 === 0 ) {
146
- weeks [ weeks . length - 1 ] . weekNumber = Math . ceil ( getDayNumber ( day . date ) / 7 )
212
+ weeks [ weeks . length - 1 ] . weekNumber = getWeekNumber ( day . date )
147
213
}
148
214
149
215
weeks [ weeks . length - 1 ] . days . push ( day )
@@ -156,6 +222,7 @@ export const isDisableDateInRange = (startDate, endDate, dates) => {
156
222
if ( startDate && endDate ) {
157
223
const date = new Date ( startDate )
158
224
let disabled = false
225
+
159
226
// eslint-disable-next-line no-unmodified-loop-condition
160
227
while ( date < endDate ) {
161
228
date . setDate ( date . getDate ( ) + 1 )
@@ -201,7 +268,9 @@ export const isDateInRange = (date, start, end) => {
201
268
}
202
269
203
270
export const isDateSelected = ( date , start , end ) => {
204
- return ( start && isSameDateAs ( start , date ) ) || ( end && isSameDateAs ( end , date ) )
271
+ return (
272
+ ( start && isSameDateAs ( start , date ) ) || ( end && isSameDateAs ( end , date ) )
273
+ )
205
274
}
206
275
207
276
export const isEndDate = ( date , start , end ) => {
@@ -211,15 +280,18 @@ export const isEndDate = (date, start, end) => {
211
280
export const isLastDayOfMonth = date => {
212
281
const test = new Date ( date . getTime ( ) )
213
282
const month = test . getMonth ( )
283
+
214
284
test . setDate ( test . getDate ( ) + 1 )
215
285
return test . getMonth ( ) !== month
216
286
}
217
287
218
288
export const isSameDateAs = ( date , date2 ) => {
219
289
if ( date instanceof Date && date2 instanceof Date ) {
220
- return ( date . getDate ( ) === date2 . getDate ( ) &&
221
- date . getMonth ( ) === date2 . getMonth ( ) &&
222
- date . getFullYear ( ) === date2 . getFullYear ( ) )
290
+ return (
291
+ date . getDate ( ) === date2 . getDate ( ) &&
292
+ date . getMonth ( ) === date2 . getMonth ( ) &&
293
+ date . getFullYear ( ) === date2 . getFullYear ( )
294
+ )
223
295
}
224
296
225
297
if ( date === null && date2 === null ) {
@@ -235,9 +307,11 @@ export const isStartDate = (date, start, end) => {
235
307
236
308
export const isToday = date => {
237
309
const today = new Date ( )
238
- return ( date . getDate ( ) === today . getDate ( ) &&
239
- date . getMonth ( ) === today . getMonth ( ) &&
240
- date . getFullYear ( ) === today . getFullYear ( ) )
310
+ return (
311
+ date . getDate ( ) === today . getDate ( ) &&
312
+ date . getMonth ( ) === today . getMonth ( ) &&
313
+ date . getFullYear ( ) === today . getFullYear ( )
314
+ )
241
315
}
242
316
243
317
export const isValidDate = date => {
0 commit comments