首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较ISO格式的日期范围

比较ISO格式的日期范围
EN

Stack Overflow用户
提问于 2020-08-20 23:21:43
回答 3查看 312关注 0票数 0

Link to codeSandox

我有两个日期范围,一个来自API,另一个来自用户输入。两种格式均为ISO格式。

来自接口的日期范围:

代码语言:javascript
复制
dateStart 2019-04-01T03:04:00Z
dateStop 2019-04-01T03:05:00Z

用户输入的日期范围:

代码语言:javascript
复制
convertedDateFrom 2020-09-15T18:30:00.000Z
convertedDateTo 2020-09-21T18:30:00.000Z

我想将date range from user input转换为date range from API。我怎样才能做到这一点呢?

代码语言:javascript
复制
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()

代码语言:javascript
复制
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,
    });
  };
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-08-21 16:26:54

您好,您可以这样使用moment提供的函数isBetween

代码语言:javascript
复制
// 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你的代码和盒子的修改。

票数 1
EN

Stack Overflow用户

发布于 2020-08-21 17:08:29

它们都是ISO-8601日期,可以很容易地转换为原生Date对象,后者可以转换为自Unix时代以来的毫秒数。为此,您不需要任何复杂的逻辑,甚至不需要使用moment

代码语言:javascript
复制
/**
 * 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))

票数 1
EN

Stack Overflow用户

发布于 2020-08-21 18:10:40

ISO日期格式的一大优点是它们可以按字母顺序排序。

这意味着,虽然您可以将其转换为Date / moment等,但您将得到与将它们作为字符串进行比较完全相同的结果。

例如,您可以将Lionel的compareISODates函数编写为:

代码语言:javascript
复制
/**
 * 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中,您可以简单地检查

代码语言:javascript
复制
convertedDateFrom >= dateStart && convertedDateTo <= dateEnd

当然,如果你需要更复杂的逻辑,你应该使用上面提到的一个日期库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63508273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档