You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've faced a necessity of checking current device's time display settings, exactly 12/24h format.
What about having such kind of extensions both for Date and String types?
extension Date {
var is12HourFormat: Bool {
let dateFormatter = DateFormatter()
let dateString = dateFormatter.string(from: self)
return dateString.contains(dateFormatter.amSymbol) || dateString.contains(dateFormatter.pmSymbol)
}
}
extension String {
/// Check if string can be formatted as date and if it has 12 hour format.
/// Returns nil if string isn't a date, and true or false if it has 12 hour format or not.
var is12HourFormat: Bool? {
let dateFormatter = DateFormatter()
let date = dateFormatter.date(from: self)
return (contains(dateFormatter.amSymbol) || contains(dateFormatter.pmSymbol)) && date != nil
}
}
The text was updated successfully, but these errors were encountered:
@DimaZava Thanks for bringing this :)
The AM or PM time is coming from the DateFormatter and not from Date, if we have the same date on different devices with different Locale we will get both is12HourFormat and is12HourFormat true for the same date 🤔
I've faced a necessity of checking current device's time display settings, exactly 12/24h format.
What about having such kind of extensions both for Date and String types?
The text was updated successfully, but these errors were encountered: