Converting Date String to different Format - swift

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

Related

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)")

How to convert a string UTC date to NSDate in Swift

I'm getting UTC string dates that look like this "2015-10-17T00:00:00.000Z" and want to convert them to an NSDate in this format "October 12th 2015 11:19:12 am"
This is the route that I'm trying but I can't seem to get the right dateFormat.
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = //can't seem to get this right
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let date = dateFormatter.dateFromString("2015-10-17T00:00:00.000Z")
I think this should work
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let localDate = formatter.date(from: date)
This works for Swift 3.0 and latest Xcode [April 2017]
let dateString = "2017-Jan-01 12:00:00.250"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-M-dd HH:mm:ss.SSS"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let dateObj = dateFormatter.date(from: dateString)
let date = dateObj
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 HH:mm:ss.SSSXXXXX"
let currentDateTime = formatter.string(from: date!)
Swift 4 code
let dateString = "2015-10-17T00:00:00.000Z"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = formatter.date(from: dateString)
You can find useful info about date formats here
This question is a few years old but let's answer it literally.
First of all DateFormatter doesn't provide a format specifier for an ordinal suffix so we have to write a function
func ordinalSuffix(for day : String) -> String {
switch day {
case "1", "11", "21", "31": return "st"
case "2", "12", "22": return "nd"
case "3", "13", "23": return "rd"
default: return "th"
}
}
To convert the iso8601 string to date create a DateFormatter, set its calendar to an iso8601 calendar and convert the string to Date. As the time zone is specified in the string you don't need to set it explicitly
let dateString = "2015-10-17T00:00:00.000Z"
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
guard let date = formatter.date(from: dateString) else {
// replace that with a proper error handling
fatalError("Could not convert date string")
}
To convert the date back to string you have to set the Locale to a fixed value, set the am/pm symbols to the lowercase versions, extract the day component first for the ordinal suffix calculation and then take advantage of String(format to insert the ordinal suffix.
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.amSymbol = "am"
formatter.pmSymbol = "pm"
formatter.dateFormat = "dd"
let day = formatter.string(from: date)
formatter.dateFormat = "MMMM dd'%#' yyyy h:mm:ss a"
let output = String(format: formatter.string(from: date), ordinalSuffix(for: day))
print(output)