Skip to content

Commit d37894d

Browse files
authored
fix(3921): should keep string types and Date types separate, and use a timestamp lib formatting. Otherwise we haxd instances where we create a date from null, and then did manual math to produce invalid dates. (#6610)
1 parent 4980b35 commit d37894d

File tree

1 file changed

+21
-32
lines changed

1 file changed

+21
-32
lines changed

src/shared/components/dateRangePicker/NewDatePicker.tsx

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, {FC, useState, useContext, useEffect, useCallback} from 'react'
2+
const moment = require('moment/min/moment.min.js')
23
import {
34
AlignItems,
45
Button,
@@ -30,6 +31,7 @@ import {TimeRange} from 'src/types'
3031

3132
const NBSP = '\u00a0\u00a0'
3233
const MAX_WIDTH_FOR_CUSTOM_TIMES = 325
34+
const FORMAT = 'YYYY-MM-DD HH:mm'
3335

3436
interface 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

Comments
 (0)