我有两个日期范围,一个来自API,另一个来自用户输入。两种格式均为ISO格式。
来自接口的日期范围:
dateStart 2019-04-01T03:04:00Z
dateStop 2019-04-01T03:05:00Z用户输入的日期范围:
convertedDateFrom 2020-09-15T18:30:00.000Z
convertedDateTo 2020-09-21T18:30:00.000Z我想将date range from user input转换为date range from API。我怎样才能做到这一点呢?
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.应与API中的日期范围重叠。
这是我的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,
});
};发布于 2020-08-21 16:26:54
您好,您可以这样使用moment提供的函数isBetween:
// 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"),因为moment({day: 19, month: 8, year: 2020}).toISOString()总是返回month + 1。
Here你的代码和盒子的修改。
发布于 2020-08-21 17:08:29
它们都是ISO-8601日期,可以很容易地转换为原生Date对象,后者可以转换为自Unix时代以来的毫秒数。为此,您不需要任何复杂的逻辑,甚至不需要使用moment。
/**
* If result is negative, the first date is earlier
* If result is positive, the second date is earlier
* If result is 0, both dates are exactly the same
*/
const compareIsoDates = (isoString1, isoString2) => {
return new Date(isoString1).valueOf() - new Date(isoString2).valueOf()
}
// result is -46106760000, meaning first date is earlier
console.log(compareIsoDates('2019-04-01T03:04:00Z', '2020-09-15T18:30:00.000Z'))
/**
* strictly between (cannot be the same as start or end)
* if you want to allow same as start and end, change to
* >= and <= instead of > and <
*/
const isStrictlyBetween = (targetDate, [startDate, endDate]) => {
return compareIsoDates(targetDate, startDate) > 0
&& compareIsoDates(targetDate, endDate) < 0
}
// true
console.log(isStrictlyBetween(
'2020-05-01T00:00:00.000Z',
['2020-04-20T18:30:00Z', '2020-05-10T00:00:00Z']
))
// you can also use `compareIsoDates` a sort function to sort an array of
// ISO strings in ascending order (earliest to latest)
console.log([
"1998-02-12T08:18:27.991Z",
"2005-03-19T19:48:59.501Z",
"1997-05-01T14:58:13.848Z",
"2008-08-31T01:30:11.880Z",
"2004-08-05T16:07:55.443Z"
].sort(compareIsoDates))
发布于 2020-08-21 18:10:40
ISO日期格式的一大优点是它们可以按字母顺序排序。
这意味着,虽然您可以将其转换为Date / moment等,但您将得到与将它们作为字符串进行比较完全相同的结果。
例如,您可以将Lionel的compareISODates函数编写为:
/**
* If result is negative, the first date is earlier
* If result is positive, the second date is earlier
* If result is 0, both dates are exactly the same
*/
const compareISODates = (isoString1, isoString2) => {
return isoString1.localeCompare(isoString2);
};或者作为另一个例子,convertedDateFrom/To完全落入dateStart/End中,您可以简单地检查
convertedDateFrom >= dateStart && convertedDateTo <= dateEnd当然,如果你需要更复杂的逻辑,你应该使用上面提到的一个日期库。
https://stackoverflow.com/questions/63508273
复制相似问题