Date formatting for next available date in Swift

396 views Asked by At

I'm currently downloading fixture lists for a sports app from a third party website that runs the league, so the data I have to work with is restricted.

I'm trying to implement a feature that displays the next upcoming fixture. My problem is the dates being retrieved look like this:

"Sat 9th Sep 17" and "Sat 24th Mar 18" for example.

I've tried numerous date formats in the DateFormatter that I know of and can't find anything that uses this specific format.

If I try to use the Date from string method in the date formatter with one of the above strings I get a nil value.

I have an array of fixtures, each with their own dates. I need to compare each of these to the current date to figure out which is next in line.

My temporary fix for this was just to loop through the fixtures and as soon as one did not have a result to display that as the next fixture. Obviously this doesn't work when a game may not have been played for whatever reason.

What would be the best way to deal with this?

1

There are 1 answers

3
AudioBubble On

Basically you would just need to convert the current date to the same format as the date you get from your third party website (or the opposite) so you can compare them easily:

    let currentDate = Date()

    let dateFormatter = DateFormatter()

    dateFormatter.dateFormat = "EE MMM"
    let firstPartStringDate = dateFormatter.string(from: currentDate)

    dateFormatter.dateFormat = "yy"
    let lastPartStringDate = dateFormatter.string(from: currentDate)

    let day = Calendar.current.component(.day, from: currentDate)

    let formatter = NumberFormatter()
    formatter.numberStyle = .ordinal

    guard let ordinalDay = formatter.string(for: day) else {
        return
    }

    let finalDateString = firstPartStringDate + " \(ordinalDay) " + lastPartStringDate

    print(finalDateString)

And for today's current date you would get the exactly same format as the one you get from the third-party website :

Sun Sep 17th 17

UPDATE: Here is how you could convert the String you get from the third-party website to a Date, and then compare it with the current date. This solves the problem of having the st, nd, rd and th inside the String at first.

// This is the string you get from the website    

    var webDateString = "Sat 9th Sep 17"

// First, remove the st, nd, rd or th if it exists :

    if let stSubrange = webDateString.range(of:"st") {
        webDateString.removeSubrange(stSubrange)
    } else if let ndSubrange = webDateString.range(of:"nd") {
        webDateString.removeSubrange(ndSubrange)
    } else if let rdSubrange = webDateString.range(of:"rd") {
        webDateString.removeSubrange(rdSubrange)
    }  else if let thSubrange = webDateString.range(of:"th") {
        webDateString.removeSubrange(thSubrange)
    }

// Now convert the string to a date :   

    dateFormatter.dateFormat = "EE MMM dd yy"
    guard let formattedDate = dateFormatter.date(from: finalDateString) else {
        return
     }

// You can now compare formattedDate and the current date easily like so :

    let currentDate = Date()

    if formattedDate < currentDate {

        // Do something interesting here :)

    } else {

        // Do something else!
    }