iosswiftdateformatter

Date format in Swift TODAY TOMORROW YESTERDAY


I want to show a date as Saturday June 13.

If the date is current day it should display Today like that Tomorrow, Yesterday.

I couldn't achieve both.

guard let date = Date(fromString: "16 September 2020",
                      format: "dd MMMM yyyy") else { return nil }
    
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.doesRelativeDateFormatting = true

header.titleLabel.text = dateFormatter.string(from: date)

For the above code I can show date as Today Tomorrow Yesterday but other dates are not showing Saturday June 13. I tried to apply date format dateFormatter.dateFormat = "EEEE, MMM d" for the same dateFormatter it returned nothing.


Solution

  • The DateFormatter doesn't behave well when setting doesRelativeDateFormatting = true and trying to apply a custom format at the same time. So the easiest solution is to use the format given by a Style and a Locale

    let relativeDateFormatter = DateFormatter()
    relativeDateFormatter.timeStyle = .none
    relativeDateFormatter.dateStyle = .medium
    relativeDateFormatter.locale = Locale(identifier: "en_GB")
    relativeDateFormatter.doesRelativeDateFormatting = true
    

    Example

    let inputFormatter = DateFormatter()
    inputFormatter.dateFormat = "yyyy-MM-dd"
    
    let dates = ["2020-09-01", "2020-09-15", "2020-09-16", "2020-09-30"].compactMap { inputFormatter.date(from: $0)}
    
    for date in dates {
        print(relativeDateFormatter.string(from: date))
    }
    

    1 Sep 2020
    Yesterday
    Today
    30 Sep 2020

    Now if you want to apply a custom format I have not found a solution for this when using the same DateFormatter instance so we need to create a new one for the custom format and use it together with a check so we apply the custom format only when it is not Today etc

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "EEEE, MMM dd"
    
    for date in dates {
        let string = relativeDateFormatter.string(from: date)
        if let _ = string.rangeOfCharacter(from: .decimalDigits) {
             print(dateFormatter.string(from: date))
        } else {
            print(string)
        }
    }
    

    Tuesday, Sep 01
    Yesterday
    Today
    Wednesday, Sep 30