File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change 16
16
"@deriv/api" : " ^1.0.0" ,
17
17
"@deriv/integration" : " ^1.0.0" ,
18
18
"@deriv/react-joyride" : " ^2.6.2" ,
19
- "@deriv/utils" : " ^1.0.0" ,
20
19
"@sendbird/chat" : " ^4.9.7" ,
21
20
"clsx" : " ^2.0.0" ,
22
21
"i18next" : " ^22.4.6" ,
Original file line number Diff line number Diff line change 1
- import { toMoment } from '@deriv/utils' ;
1
+ import moment from 'moment' ;
2
+
3
+ /**
4
+ * Function that converts a numerical epoch value into a Moment instance
5
+ */
6
+ export const epochToMoment = ( epoch : number ) => moment . unix ( epoch ) . utc ( ) ;
7
+
8
+ /**
9
+ * Function that takes a primitive type and converts it into a Moment instance
10
+ */
11
+ export const toMoment = ( value ?: moment . MomentInput ) : moment . Moment => {
12
+ if ( ! value ) return moment ( ) . utc ( ) ; // returns 'now' moment object
13
+ if ( moment . isMoment ( value ) && value . isValid ( ) && value . isUTC ( ) ) return value ; // returns if already a moment object
14
+ if ( typeof value === 'number' ) return epochToMoment ( value ) ; // returns epochToMoment() if not a date
15
+
16
+ if ( / i n v a l i d / i. test ( moment ( value ) . toString ( ) ) ) {
17
+ const todayMoment = moment ( ) ;
18
+ const daysInMonth = todayMoment . utc ( ) . daysInMonth ( ) ;
19
+ const valueAsNumber = moment . utc ( value , 'DD MMM YYYY' ) . valueOf ( ) / ( 1000 * 60 * 60 * 24 ) ;
20
+ return valueAsNumber > daysInMonth
21
+ ? moment . utc ( todayMoment . add ( value . valueOf ( ) , 'd' ) , 'DD MMM YYYY' )
22
+ : moment . utc ( value , 'DD MMM YYYY' ) ; // returns target date
23
+ }
24
+ return moment . utc ( value ) ;
25
+ } ;
2
26
3
27
/**
4
28
* return the number of days since the date specified
You can’t perform that action at this time.
0 commit comments