Closest date to today [SOLVED]


#1

Hi everyone!

I have a JSON with this data in it:

[
    {   "eventName":"Name 1",
        "races": [
            {
                "raceName": "free rides",
                "raceDate": "2017-12-28 05:00:00 pm"
            },
            {
                "raceName": "qualification",
                "raceDate": "2017-12-29 05:00:00 pm"
            },
            {
                "raceName": "race",
                "raceDate": "2017-12-30 05:00:00 pm"
            }
        ]
        
    },
    {   "eventName":"Name 2",
        "races": [
            {
                "raceName": "free rides",
                "raceDate": "2018-01-02 05:00:00 pm"
            },
            {
                "raceName": "qualification",
                "raceDate": "2018-01-03 05:00:00 pm"
            },
            {
                "raceName": "race",
                "raceDate": "2018-01-04 05:00:00 pm"
            }
        ]
        
    }
]

And I need to get the closest race to today, based on “raceDate”. How can I achieve this?

Thank you.
Olga


#2

See:


#3

Sorry, I should have been more explicit. I know how to access the “raceDate” for each race:

for(let i = 0; i < this.raceData.length; i++) {
            
            for(let l=0; l < this.raceData[i].races.length; l++) {
                this.raceDates = this.raceData[i].races[l].raceDate;
                console.log(JSON.stringify(this.raceDates));
            }
            
        }

My trouble is how to compare those dates to today and get the closest one.


#4

When working with dates you can use moment.js or date-fns.

See


#5

Agree with @robinyo you could use https://date-fns.org/ or https://momentjs.com/ to work with Date

Otherwise, I would still advice to use lib but still if you want to do it by your own, not sure it will works, but you could use Date and getTime for comparison

For example to guess if a date is bigger than today (in the future)

let today: Date = new Date();

// Here I'm not sure that it will works respectively that your json date format will be supported for parsing
let date1: Date = new Date(this.raceData[0].races[0].raceDate);

let diff1: number = today.getTime() - date1.getTime();

console.log('Is date1 in the future? ' + (diff1 < 0));

#6

I decided not to use any additional library, my solution is:

for(let i = 0; i < this.raceData.length; i++) {
    if(this.closestFound == true) {
        break;
    }
    for(let l=0; l < this.raceData[i].races.length; l++) { 
        // if session is in the future   
        if(new Date(this.raceData[i].races[l].raceDate) > this.today) {
            this.activeEventIndex = i;
            this.activeEvent = this.raceData[i];
            this.activeSession = this.raceData[i].races[l];
            this.closestFound = true;
            break;
         }else{
            this.raceData[i].races[l].status = "disabled";
         }
     }
}

Thanks for all replies!


#7

@Smirnova in your comparison I would still advice to compare Date with time, like

if(new Date(this.raceData[i].races[l].raceDate.getTime()) > this.today.getTime()) {

I don’t remember exactly why, maybe because of the internationalization, but back then I came to the conclusion than comparing dates with time is more secure/stable…just a thought