Skip to content

Commit

Permalink
make use of startOfDay
Browse files Browse the repository at this point in the history
  • Loading branch information
joakbjerk committed Jan 5, 2024
1 parent d9fcf8b commit 705676c
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions packages/dnb-eufemia/src/components/date-picker/DatePickerCalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import getDaysInMonth from 'date-fns/getDaysInMonth'
import toDate from 'date-fns/toDate'
import parseISO from 'date-fns/parseISO'
import parse from 'date-fns/parse'
import startOfDay from 'date-fns/startOfDay'

import { warn } from '../../shared/component-helper'

Expand Down Expand Up @@ -170,21 +171,14 @@ const isWithinSelectionCalc = (date, startDate, endDate) => {
// date is before minDate or after maxDate
const isDisabledCalc = (date, minDate, maxDate) => {
// isBefore and isAfter return false if comparison date is undefined, which is useful here in case minDate and maxDate aren't supplied
// resetting the the `minDate` and `maxDate` hours for comparison, as the `date` param comes with a time of 00:00:00

return (
(minDate && isBefore(date, resetHours(minDate))) ||
(maxDate && isAfter(date, resetHours(maxDate)))
(minDate && isBefore(date, startOfDay(minDate))) ||
(maxDate && isAfter(date, startOfDay(maxDate)))
)
}
export { isDisabledCalc as isDisabled }

// useful for comparing dates that strictly speaking are the same, but differs in time (hours, minutes, seconds)
// returns a new Date object to prevent mutation
const resetHours = (date) => {
const dateCopy = new Date(date)
return dateCopy.setHours(0, 0, 0, 0)
}

// date selected is start date
const isStartDateCalc = (date, range) => {
return range.startDate && isSameDay(date, range.startDate)
Expand Down

0 comments on commit 705676c

Please sign in to comment.