简体   繁体   English

如何计算日期对象是否更接近一个或另一个日期

[英]How can I calculate if a Date Object is closer to one or another Date

I am building a calendar where people can select a range of days from a visual calendar. 我正在建立一个日历,人们可以从视觉日历中选择一个日期范围。

Clicking on a date will select it as a "from" or "to" date. 单击一个日期会将其选择为“开始”或“结束”日期。 For example, if I click on 1januari and 5 January. 例如,如果我单击1januari和1月5日。 1 January will be the "from" date while 5 January will be the "to" date. 1月1日为“开始”日期,而1月5日为“开始”日期。 Easy right? 容易吧?

Now the thing is if a user clicks on a new date, I will need to update the current "from" and "to" dates. 现在的问题是,如果用户单击一个新日期,则需要更新当前的“开始”和“结束”日期。 Using 使用

stringToDate(date.dateTime) > stringToDate(this.state.fromDate)

lets me know if the incoming date is later then the current "from date" with I can use to change the current "to" date to the incoming Date because 7 January comes after 5 January with "must" mean the user wanted to update their "to" date. 让我知道输入的日期是否晚于当前的“起始日期”,我可以用它来将当前的“到”日期更改为输入的日期,因为1月7日在1月5日之后,“必须”表示用户想要更新其日期“至今。

A similar function does the same thing for the "from" date. 类似的功能对于“起始”日期执行相同的操作。 This works great and all but this means that users can't select a "from" date that comes later than their original "from" date. 这很好用,但所有这一切都意味着用户不能选择比其原始“开始”日期晚的“开始”日期。

What I want to achieve is the following: Calculate if the incoming date is closer to the "from" date or the "to" date. 我要实现的目标如下:计算传入的日期是否更接近“起始”日期或“终止”日期。 So if a user starts with 1 and 5 January and clicks on 2 January I can assume that he wants to update his "from" date. 因此,如果用户从1月1日至5日开始并在1月2日单击,我可以假定他要更新其“开始”日期。

My current function looks like this: 我当前的函数如下所示:

handleFromDateSelected(date) {
    let updatedFromDate = '';
    let updatedToDate = '';

    if (this.state.fromDate) {
        if (stringToDate(date.dateTime) > stringToDate(this.state.fromDate)) {
            updatedFromDate = this.state.fromDate;
            updatedToDate = date.dateTime;
        } else {
            updatedFromDate = date.dateTime;
            updatedToDate = this.state.fromDate;
        }
    } else {
        updatedFromDate = date.dateTime;
    }

    this.setState({
        fromDate: updatedFromDate,
        toDate: updatedToDate,
    });
}

If you really want to check from which date the new one is closed, which is, in my opinion not a good choice, you can convert all 3 dates (from, to and new) to timestamp and compare the absolute difference. 如果您真的想检查新日期的关闭日期,我认为这不是一个好选择,则可以将所有3个日期(从,到,再到新)转换为时间戳并比较绝对差值。 The lowest one means this is closer to the new one. 最低的表示这更接近新的。

in example, let's assume dateFrom , dateTo and newDate are date objects : 例如,假设dateFromdateTonewDate是日期对象:

//Returns true if newDate is closer to dateFrom
function isFromDateCloser(dateFrom, dateTo, newDate)
{
    timeStampFrom = dateFrom.getTime();
    timeStampTo = dateTo.getTime();
    timeStampNew = newDate.getTime();

    return (Math.abs(timeStampFrom - timeStampNew) < Math.abs(timeStampTo - timeStampNew));
}

if (isFromDateCloser(dateFrom, dateTo, newDate))
    dateFrom = newDate;
else
    dateTo = newDate;

In that example, I've arbitrary choosen that if new date is strictly between both dates, the new date will be dateTo. 在该示例中,我任意选择,如果新日期严格位于两个日期之间,则新日期将为dateTo。 If you prefer the that the from become the new date, replace in the return < by <= 如果你喜欢的是,从成为新的日期,替换回报<通过<=

In your case 就你而言

 var firstDate = new Date(date.dateTime);
 var secondDate = new Date(this.state.fromDate)
          if (firstDate.getTime() > secondDate .getTime()){
  // do something here 
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM