Reputation: 37
I hardly try but can't find how to compare am and pm.
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "h:mm a"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
dateString = formatter.string(from: Date())
// date Format
dateFormatter.timeStyle = DateFormatter.Style.none;
dateFormatter.dateStyle = DateFormatter.Style.medium;
currentDate = dateFormatter.string(from: now);
if self.dateString >= "09:00 AM" { return true }
Upvotes: 1
Views: 1187
Reputation: 345
In iOS, Apple provide compare function to compare to Date objects. Please find the below code to compare time
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "h:mm a"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
let timeString = formatter.string(from: Date())
let firstTime = formatter.date(from: "09:00 AM")
let secondTime = formatter.date(from: dateString)
if firstTime?.compare(secondTime!) == .orderedAscending {
print("First Time is smaller then Second Time")
}
Upvotes: 2