简体   繁体   中英

How can I compare two dates in Swift 3.2?

I am trying to compare two dates. One date is from the server and the other one is today's date.

for i in 0...self.totalDaysFromServerArray.count-1 {
    let selectedDate = self.totalDaysFromServerArray[i] //selectedDate: "12/12/2017 23:12"

    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd-MM-yyyy HH:MM"
    let date = dateFormatter.date(from: }selectedDate)
}

if Calendar.current.isDate((date)!, inSameDayAs:todayDate as Date) == true {

}

Printing the description of dates:

▿ Optional<Date>
  ▿ some : 2017-12-29 17:30:00

  let todayDate = Date() 
//Printing description of todayDate:
▿ 2017-12-29 11:50:45 

If both dates are same, I have to do some execution inside the if condition in the end.
But that loop is never executing, even when both are same dates. Any suggestions?

The problem is that you are not interpreting the timezone properly. The dates strings from the server are probably UTC not local time so you need to set your dateFormatter timezone to zero seconds from GMT TimeZone(secondsFromGMT: 0) . Second problem is that you are using MM which it is for month in the minutes "HH:mm" . Third issue is that you also forgot to use "/" instead of "-" as the separator, the correct fixed date format for your date strings should be "dd/MM/yyyy HH:mm" . Fourth when parsing fixed date format you need to set dateFormatter locale to "en_US_POSIX" . Is it an array or a dictionary? 6 : "18/12/2017 23:12" looks like a [Int:String] dictionary. You need also to move your if condition inside your loop and there is no date in your comment above that matches today, so I will add one that matches just for demonstration purposes.

let totalDaysFromServerArray = ["18/12/2017 23:12", "19/12/2017 23:12", "20/12/2017 23:12", "21/12/2017 23:12", "29/12/2017 23:12"]
for dateString in totalDaysFromServerArray {
    print("server:", dateString) 
    let dateFormatter = DateFormatter()
    dateFormatter.locale = Locale(identifier: "en_US_POSIX")
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
    dateFormatter.dateFormat = "dd/MM/yyyy HH:mm"
    if let date = dateFormatter.date(from: dateString) {
        print("parsed:", dateFormatter.string(from: date))
        print("match:", dateString == dateFormatter.string(from: date))
        if Calendar.current.isDate(date, inSameDayAs: Date()) {
            print("inSameDayAsToday:", true)
        }
    }
}

Use:

dateFormatter.dateFormat = "dd-MM-yyyy HH:mm"

Capital M is used for months not minutes

As far as I understood you want to exclude hours and minutes from your formatting. Try the following code:

dateFormatter.timeStyle = DateFormatter.Style.none
dateFormatter.dateStyle = DateFormatter.Style.short

dateFormatter.string(from: date)

This outputs "12/29/17" as of today. You can get the dates without hours:minutes this way and then compare.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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