iOS 15.4 Date Format "Sep" now "Sept" - ios

The app crashes once the user selects any date of September.
It only happens with iOS 15.4.
I have datePicker formatted date to return always in UTC format en_GB.
#objc func doneDateClicked() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none //CHANGED 09.07.2020 date medium time none
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.timeZone = TimeZone(identifier: "GMT")
dateFormatter.locale = Locale(identifier: "en_GB")
dateFormatter.dateFormat = "dd MMM yyyy"
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .medium
timeFormatter.timeZone = TimeZone(secondsFromGMT: 0)
timeFormatter.timeZone = TimeZone(identifier: "GMT")
timeFormatter.locale = Locale(identifier: "en_150")
dateTextField.text = dateFormatter.string(from: (datePicker?.date )!)
self.view.endEditing(true)
}
Then I need to covert the selected date from string (dateTextField.text) to date
on iOS 15.4 the month September it shows "Sept".
code to convert from string to date
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_GB")
formatter.dateFormat = "dd MMM yyyy hh:mm:ss"
formatter.calendar = Calendar(identifier: .gregorian)
let datenN = formatter.date(from: "\(dateTextField.text ?? "") 00:00:00")
datenN crashing the app it returns nil
I tried to use code formatter.locale = Locale(identifier: "en_US_POSIX") but the app still crashing

Assuming this problem occurs just for September and on iOS 15.4, then
a quick work around, is to replace "Sept" with "Sep", using
dateTextField.text = dateFormatter.string(from: (datePicker?.date )!)
if dateTextField.text.contains("Sept") {
dateTextField.text = dateTextField.text.replacingOccurrences(of: "Sept", with: "Sep")
}

Related

Returning nil when converting string to date in swift

My date format in string is "2019-03-28".
My date formatter is like "yyyy-MM-dd"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: deliveryDate)
What am I doing wrong here
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: "2019-03-28")
print("assignDate \(assignDate)")
All works fine for me here!
assignDate Optional(2019-03-28 00:00:00 +0000)
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+0:00")
let assignDate = dateFormatter.date(from: deliveryDate)
This is working fine

DateFormatter from "24:00" string returns Nil

I cannot seem to figure out why only "24:00" returns nil instead of 12:00AM, whereas "23:59" returns correctly as 11:59PM.
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
let date = dateFormatter.date(from: dateAsString)
dateFormatter.dateFormat = "h:mm a"
let Date12 = dateFormatter.string(from: date!)
print(Date12)
You are using the wrong date format for the hour component. For values from 1-24 you have to use k, if you have a leading zero you need to use kk:
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "kk:mm"
if let date = dateFormatter.date(from: dateAsString) {
dateFormatter.dateFormat = "h:mm a"
let date2 = dateFormatter.string(from: date)
print(date2) // "12:00 AM\n"
}

Why doesn't this time conversion from UTC to local code work?

let dateFormatter = DateFormatter()
let dateFormatter2 = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: event!)
dateFormatter2.timeZone = TimeZone.current
let dateString = dateFormatter2.string(from: date!)
let finalDate = dateFormatter2.date(from: dateString)
return finalDate!
I have sports events that give me string dates in UTC which I am trying to convert into local time. This code is similar to the dozens of examples given in this site when asked this question, yet it doesn't work.
This is the iso8601 formatter :
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
return formatter
}()
This turns your string into a Date.
You will need to add the time difference, most likely through TimeZone.autoupdatingCurrent

Put the time in French

I try to format the time in French in my iOS app written in Swift 3
Here the data string
2000-01-01T14:00:00.000Z
Here method swift 3
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm"
return dateFormatter.string(from: timeStart!);
}
On my application gives its 2:00
And I want to display 2:00 p.m.
I used it: dateFormatter.locale = Locale (identifier: "fr_FR")
but its not working there have another manipulation do?
You need to fix both date formats:
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.string(from: timeStart!);
}
Better yet, don't use dateFormat to convert the Date to a String. Instead, set the timeStyle so it's formatted properly without the need to force a specific format.
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = nil
dateFormatter.timeStyle = .medium
dateFormatter.dateStyle = .none
return dateFormatter.string(from: timeStart!);
}
In either case, only set the locale if you want all users to see the time formatted in that specific locale (and that would be rare).

NSDate to String with TimeZone Offset

I've the following code
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.stringFromDate(NSDate())
It produces date in the following format:
2015-08-06T15:44:49+0000
But I want it to be in the following format 2015-08-06T15:44:49+00:00
As you can see the offset needs to b +00:00 instead of +0000
You need to use "xxx" instead of "Z".
edit/update Swift 3 or later:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxx"
dateFormatter.string(from: Date()) // "2019-03-22T14:36:07+00:00"
If you need some reference you can use this:

Resources