1
1
const chrono = require ( 'chrono-node' ) ;
2
- const moment = require ( 'moment' ) ;
2
+ const moment = require ( 'moment-timezone ' ) ;
3
3
const UserError = require ( './UserError' ) ;
4
4
5
- module . exports = ( dateString ) => {
5
+ module . exports = ( dateString , timezone ) => {
6
6
let momentRange ;
7
7
dateString = dateString . toLowerCase ( ) ;
8
8
if ( dateString . match ( / ( t h i s | l a s t ) \s + ( d a y | w e e k | m o n t h | y e a r | q u a r t e r | h o u r | m i n u t e | s e c o n d ) / ) ) {
9
9
const match = dateString . match ( / ( t h i s | l a s t ) \s + ( d a y | w e e k | m o n t h | y e a r | q u a r t e r | h o u r | m i n u t e | s e c o n d ) / ) ;
10
- let start = moment ( ) ;
11
- let end = moment ( ) ;
10
+ let start = moment . tz ( timezone ) ;
11
+ let end = moment . tz ( timezone ) ;
12
12
if ( match [ 1 ] === 'last' ) {
13
13
start = start . add ( - 1 , match [ 2 ] ) ;
14
14
end = end . add ( - 1 , match [ 2 ] ) ;
@@ -19,25 +19,25 @@ module.exports = (dateString) => {
19
19
const match = dateString . match ( / l a s t \s + ( \d + ) \s + ( d a y | w e e k | m o n t h | y e a r | q u a r t e r | h o u r | m i n u t e | s e c o n d ) / ) ;
20
20
const span = match [ 2 ] === 'week' ? 'isoWeek' : match [ 2 ] ;
21
21
momentRange = [
22
- moment ( ) . add ( - parseInt ( match [ 1 ] , 10 ) - 1 , match [ 2 ] ) . startOf ( span ) ,
23
- moment ( ) . add ( - 1 , match [ 2 ] ) . endOf ( span )
22
+ moment . tz ( timezone ) . add ( - parseInt ( match [ 1 ] , 10 ) - 1 , match [ 2 ] ) . startOf ( span ) ,
23
+ moment . tz ( timezone ) . add ( - 1 , match [ 2 ] ) . endOf ( span )
24
24
] ;
25
25
} else if ( dateString . match ( / t o d a y / ) ) {
26
- momentRange = [ moment ( ) . startOf ( 'day' ) , moment ( ) . endOf ( 'day' ) ] ;
26
+ momentRange = [ moment . tz ( timezone ) . startOf ( 'day' ) , moment . tz ( timezone ) . endOf ( 'day' ) ] ;
27
27
} else if ( dateString . match ( / y e s t e r d a y / ) ) {
28
- const yesterday = moment ( ) . add ( - 1 , 'day' ) ;
28
+ const yesterday = moment . tz ( timezone ) . add ( - 1 , 'day' ) ;
29
29
momentRange = [ moment ( yesterday ) . startOf ( 'day' ) , moment ( yesterday ) . endOf ( 'day' ) ] ;
30
30
} else {
31
31
const results = chrono . parse ( dateString ) ;
32
32
if ( ! results ) {
33
33
throw new UserError ( `Can't parse date: '${ dateString } '` ) ;
34
34
}
35
35
momentRange = results [ 0 ] . end ? [
36
- results [ 0 ] . start . moment ( ) ,
37
- results [ 0 ] . end . moment ( )
36
+ moment ( results [ 0 ] . start . moment ( ) ) . tz ( timezone ) . startOf ( 'day' ) ,
37
+ moment ( results [ 0 ] . end . moment ( ) ) . tz ( timezone ) . endOf ( 'day' )
38
38
] : [
39
- results [ 0 ] . start . moment ( ) ,
40
- results [ 0 ] . start . moment ( )
39
+ moment ( results [ 0 ] . start . moment ( ) ) . tz ( timezone ) . startOf ( 'day' ) ,
40
+ moment ( results [ 0 ] . start . moment ( ) ) . tz ( timezone ) . endOf ( 'day' )
41
41
] ;
42
42
}
43
43
return momentRange . map ( d => d . format ( moment . HTML5_FMT . DATETIME_LOCAL_MS ) ) ;
0 commit comments