Date formatter crashes romanian language in Xcode - swift

Below is the code to convert the string to date, which works perfectly for other languages but doesn't work when I change the iPad language to Romanian. Basically, it crashes when it finds it nil in the 5th line. But in English or any other language, this code perfectly converts to date.Any thought?
let dateOfProcedure = "03. Jun 2021 15:12:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd. MMM yyyy HH:mm:ss"
dateFormatter.timeZone = .current
let examDate = dateFormatter.date(from:dateOfProcedure)!

You need to set the locale for the formatter to one that matches the input. Assuming it is English you can use
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
Or use some other identifier where the language is English. Then if you want to convert it to another language you need a second formatter with the locale set to the language you want and use it to format a string from your Date object (examDate)
Below is a full example that parses the string and then format it into a Swedish date
let dateOfProcedure = "03. Jun 2021 15:12:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "dd. MMM yyyy HH:mm:ss"
dateFormatter.timeZone = .current
if let examDate = dateFormatter.date(from:dateOfProcedure) {
let outputFormatter = DateFormatter()
outputFormatter.locale = Locale(identifier: "sv_SE")
outputFormatter.dateStyle = .full
print(outputFormatter.string(from: examDate))
} else {
//Handle formatting error
}

Related

Converting Date String to different Format

I have myString "28-OCT-22"
I need to convert it to different format "dd.MM.yyyy"
What I've tried:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "d-MMM-yy"
let date = dateFormatter.date(from: myString)! //error, I think because OCT is uppercase, NSDateFormatter doesn't have this format?
dateFormatter.dateFormat = "dd.MM.yyyy"
let stringDate = dateFormatter.string(from: date)
and is there more clean way to do this?
Month abbreviations are localized.
Set the locale to the fixed generic value en_US_POSIX which supports OCT
let myString = "28-OCT-22"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "d-MMM-yy"
let date = dateFormatter.date(from: myString)!
dateFormatter.dateFormat = "dd.MM.yyyy"
let stringDate = dateFormatter.string(from: date)
Although the format is correct force unwrapping converted dates is not recommended

Date format ( "yyyy-MM-dd HH:mm:ss VV") returns nil

I'm trying to change format my date for in-app purchases but returns nil in real devices but in simulator it working great.
let DateString = "2016-01-21 00:29:09 Etc/GMT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
var dateObject = dateFormatter.date(from: DateString)
print(dateObject)
You should always set the locale of your DateFormatter when dealing with hardcoded Date formats. You can use en_US_POSIX in most scenarios.
let dateString = "2016-01-21 00:29:09 Etc/GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss VV"
let dateObject = dateFormatter.date(from: dateString)
print(dateObject)
I solved like this. maybe it's helps
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"

Formatting a date in Russian in Swift

How to translate the value "12-December", only translated into Russian for further comparison of the months?
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMMM"
let stringDate = dateFormatter.string(from: NSDate() as Date)
print(stringDate)
In case that you need month names like "январь" and not "января"
(если нужен именительный падеж = if you neeed a nominative case)
you need to use LLLL instead of MMMM
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ru_RU")
dateFormatter.dateFormat = "LLLL"
let stringDate = dateFormatter.string(from: Date())
print(stringDate)
Set the date formatter's locale to a Russian locale.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ru_RU")
dateFormatter.dateFormat = "dd-MMMM"
let stringDate = dateFormatter.string(from: Date())
print(stringDate)
Result:
12-декабря
If you want to see all the months in Russian, simply do:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "ru_RU")
print(dateFormatter.monthSymbols)
Result:
["января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря"]
Also note there is no need to use NSDate, just use Date.

DateFormatter returns nil

Why does DateFormatter return nil?
I think the string format matches?
let dateString = ("01/05/2017")!
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone.current
dateFormatter.dateFormat = "dd/MMM/yyyy"
let dateObj = dateFormatter.date(from: dateString)
After executing the code above, the dateObj is nil.
For month you need to use MM because MMM is used when you having month in the format like Jan,Feb,Mar and so on. So your dateFormat should be dd/MM/yyyy.
let dateString = "01/05/2017"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateObj = dateFormatter.date(from: dateString)
Note: No need to set timeZone to TimeZone.current
Replace with your dateFormat:
dateFormatter.dateFormat = "dd/MM/yyyy"
date Formatter should be dateFormatter.dateFormat = "dd/MM/yyyy" in this case.
You can check various date format from given link
NSDateFormatter
For given example you can use let formaterStrig = "dd/MM/yyyy"

How to convert "2017-01-09T11:00:00.000Z" into Date in Swift 3?

My problem is that the date is nil.
My code looks like
print(article_date) // output "2017-01-09T11:00:00.000Z" as string
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
let date : Date? = dateFormatter.date(from: article_date!)
print("date: \(date)")
I've tried a few formatters like "yyyy-MM-dd'T'HH:mm:ssZ", but nothing worked.
You need to set both .SSS and Z with your dateFormat, so your dateFormat should be yyyy-MM-dd'T'HH:mm:ss.SSSZ.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2017-01-09T11:00:00.000Z")
print("date: \(date)")