11import React , { FC , useState , useContext , useEffect , useCallback } from 'react'
2+ const moment = require ( 'moment/min/moment.min.js' )
23import {
34 AlignItems ,
45 Button ,
@@ -30,6 +31,7 @@ import {TimeRange} from 'src/types'
3031
3132const NBSP = '\u00a0\u00a0'
3233const MAX_WIDTH_FOR_CUSTOM_TIMES = 325
34+ const FORMAT = 'YYYY-MM-DD HH:mm'
3335
3436interface Props {
3537 onCollapse : ( ) => void
@@ -118,8 +120,17 @@ const DatePickerMenu: FC<Props> = ({onCollapse, timeRange, timeRangeLabel}) => {
118120 setInputEndDate ( value )
119121 }
120122
121- const handleSelectDate = ( dates ) : void => {
123+ const handleSelectDate = ( dates : [ Date , Date ] ) : void => {
122124 const [ start , end ] = dates
125+ // end should be EOD
126+ end && end . setMinutes ( 59 )
127+ end && end . setHours ( 23 )
128+
129+ // clone mutable objects
130+ const inputStart = new Date ( start )
131+ const inputEnd = new Date ( end )
132+
133+ // set local state
123134 if ( timeZone === 'UTC' ) {
124135 if ( start ) {
125136 start . setMinutes ( start . getMinutes ( ) + start . getTimezoneOffset ( ) )
@@ -129,38 +140,16 @@ const DatePickerMenu: FC<Props> = ({onCollapse, timeRange, timeRangeLabel}) => {
129140 }
130141 }
131142 setDateRange ( [ start , end ] )
132- let startInput = start
133- let endInput = end
134-
135- const pad = part => part . toString ( ) . padStart ( 2 , '0' )
136- if ( startInput instanceof Date ) {
137- const dateParts = [
138- startInput . getFullYear ( ) ,
139- startInput . getMonth ( ) + 1 ,
140- startInput . getDate ( ) ,
141- ]
142- . map ( pad )
143- . join ( '-' )
144- const timeParts = [ startInput . getHours ( ) , startInput . getMinutes ( ) ]
145- . map ( pad )
146- . join ( ':' )
147- startInput = `${ dateParts } ${ timeParts } `
143+
144+ // format for input display in DatePicker
145+ let startInput : string = null
146+ let endInput : string = null
147+
148+ if ( start instanceof Date ) {
149+ startInput = moment ( inputStart ) . format ( FORMAT )
148150 }
149- if ( endInput instanceof Date ) {
150- const endDate = new Date ( endInput )
151- // this sets the time to the end of the selected end day
152- endDate . setMinutes ( endDate . getMinutes ( ) - 1 )
153- const dateParts = [
154- endDate . getFullYear ( ) ,
155- endDate . getMonth ( ) + 1 ,
156- endDate . getDate ( ) + 1 ,
157- ]
158- . map ( pad )
159- . join ( '-' )
160- const timeParts = [ endDate . getHours ( ) , endDate . getMinutes ( ) ]
161- . map ( pad )
162- . join ( ':' )
163- endInput = `${ dateParts } ${ timeParts } `
151+ if ( end instanceof Date ) {
152+ endInput = moment ( inputEnd ) . format ( FORMAT )
164153 }
165154 setInputStartDate ( startInput )
166155 setInputEndDate ( endInput )
0 commit comments