I have two date ranges, one from API and another from user input. Both are in ISO format.
date range from API:
dateStart 2019-04-01T03:04:00Z
dateStop 2019-04-01T03:05:00Z
date range from user input:
convertedDateFrom 2020-09-15T18:30:00.000Z
convertedDateTo 2020-09-21T18:30:00.000Z
I want to convert the date range from user input
to date range from API
. How can I achieve that?
EXPECTED: I want to compare the values two date-ranges and depending on that
I will perform certain export functionality.
The user input date-range could
- fall completely within the date-range of the API
- or at least one of the date values could fall or overlap within the
date-range from the API.
should overlap the date range from the API.
this is my date range picker handle()
handleDatePickerChange = (setSelectedDayRange) => {
console.log("initializing handleDatePickerChange()");
console.log("setSelectedDayRange", setSelectedDayRange);
// TODO
// convert the dates
let convertedDateFrom = moment(setSelectedDayRange.from).toISOString();
console.log("convertedDateFrom", convertedDateFrom);
let convertedDateTo = moment(setSelectedDayRange.to).toISOString();
console.log("convertedDateTo", convertedDateTo);
// compare dates
// if(convertedDateFrom === )
// check if data exists
this.setState({
selectedDayRange: setSelectedDayRange,
});
};
Ciao, you could use function isBetween
provided by moment in this way:
// interval comes from API
let dateAPIFrom = moment().toISOString();
let dateAPITo = moment().add(2, "days").toISOString();
// user date interval
let convertedDateFrom = moment(setSelectedDayRange.from).toISOString();
let convertedDateTo = moment(setSelectedDayRange.to).toISOString();
if (
moment(convertedDateFrom)
.subtract(1, "month")
.isBetween(dateAPIFrom, dateAPITo) &&
moment(convertedDateTo)
.subtract(1, "month")
.isBetween(dateAPIFrom, dateAPITo)
) {
//The user input date-range fall completely within the date-range of the API
} else if (
moment(convertedDateFrom)
.subtract(1, "month")
.isBetween(dateAPIFrom, dateAPITo) ||
moment(convertedDateTo)
.subtract(1, "month")
.isBetween(dateAPIFrom, dateAPITo)
) {
//or at least one of the date values could fall or overlap within the date-range from the API.
}
.subtract(1, "month")
because moment({day: 19, month: 8, year: 2020}).toISOString()
returns always month + 1
.
Here your codesandbox modified.