0

I want to compare two dates to identify which date is older. The dates are stored in String format like

let stringA = "2022-11-14"
let stringB = "2022-11-13"

I can asure that all dates will be saved in this String format: yyyy-MM-dd

In my locale testing I was able to run

let compare = stringA > stringB

With various dates I always got the correct result true/false.

Is this the correct way of comparing those dates? Or should I use a DateFormatter to compare all those dates in Date format?

P.S. I know how two convert those Strings to Dates and compare them afterwards. I just want to avoid it if possible because the String comparison is shorter, easy to read and would save redundant code if possible.

Sonius
  • 1,597
  • 3
  • 14
  • 33

1 Answers1

0

you can convert string to date and compare easily just add following String extension in your code

  extension String {
    func toDate() -> Date? {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd" //you can add any date formatter I just added according to your question formate
        return formatter.date(from: self)
    }
}

Use it like this in ViewController

 class ViewController: UIViewController {
        
    let stringA = "2022-11-13"
    let stringB = "2022-11-13"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        guard let date1 = stringA.toDate(), let date2 = stringB.toDate() else {
            print("not able to convert to date")
            return
        }
        
        let result = date1.compare(date2)
        switch result {
        case .orderedAscending:
            print("Ascending")
        case .orderedDescending:
            print("Descending")
        case .orderedSame:
            print("same")
        }
    }
}

you can check this answer for different date formatters.

Naqeeb
  • 1,121
  • 8
  • 25
  • 1
    You can, but it's redundant, the format `yyyy-MM-dd` is perfectly comparable. – vadian Nov 22 '22 at 10:24
  • I know I can and how to do it. The question is more if I have to as it saves a lot of code if I don't. – Sonius Nov 22 '22 at 11:00
  • Converting them to `Date` is actually very problematic. The string contains only partial date-time information ("the day") while `Date` represents the whole date-time information. When parsing "day" to `Date`, the time information has to be added, which is usually set to current time. That means that two equal string values can be converted to different `Date` instances. Also, once converting string to date, all problems like locale, time zone, daylight saving time etc. start appearing. Keep them as strings or do it correctly. – Sulthan Nov 22 '22 at 11:14
  • @Sulthan agree with your opinion but I think if we convert with same formatter in local it won't create any issue. anyways so it safe to go directly with string comparison ? – Naqeeb Nov 22 '22 at 11:21
  • @NaqeebAhmed Is it safe comparing strings since the comparison is well defined and it's actually the reason why we use date formats like that. You are wrong. Converting it in local will create issues. – Sulthan Nov 22 '22 at 11:23