In the Hebrew calendar, the days in the month are text.
For example, the first in "Sivan" (name of a month) is written "א" in Hebrew (and not "1" like in the Gregorian calendar).
This is my code:
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .hebrew)
formatter.dateFormat = "d"
print(formatter.string(from: date)) // "1"
But unfortunately, instead of getting א I'm getting 1.
I wanted to know if there's a way to get it as it should have been? Thanks
I initially thought it was just a matter of setting Locale on the DateFormatter, but turns out a date style was also needed to be set:
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .hebrew)
formatter.locale = Locale(identifier: "he") // <- this
formatter.dateStyle = .short // <- and this
formatter.dateFormat = "dd" // after dateStyle
print(formatter.string(from: Date())) // כ״ט
Related
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
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
}
I am trying to print a date (e.g. a birthdate) using the following code (swift 4.0, Xcode 10.2):
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateStyle = .short
formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let DOB = formatter.date(from: "2019/05/01 22:30")
when print(DOB) I got, "2019-05-02 05:30:00 +0000"
It looks like none of the Local, .short settings affected the results.
What's wrong. How do I get to simply print: "2019/05/01 22:30"?
What you are currently doing is converting a string to a Date. Dates don't contain information about how they are displayed. They are printed all the same way: 2019-05-02 05:30:00 +0000.
Strings have formats, so you should convert the Date you just got, back to a string using a date formatter. Basically, you have two formatters: one for converting the string to a date, and another for converting the date back to a string
let stringToDateformatter = DateFormatter()
stringToDateformatter.dateFormat = "yyyy/MM/dd HH:mm"
let DOB = stringToDateformatter.date(from: "2019/05/01 22:30")
let dateToStringFormatter = DateFormatter()
dateToStringFormatter.timeStyle = .short
dateToStringFormatter.dateStyle = .short
dateToStringFormatter.locale = Locale(identifier: "en_US")
let DOBString = dateToStringFormatter.string(from: DOB!)
print(DOBString) // 5/1/19, 10:30 PM
let formatter = DateFormatter()
formatter.timeStyle = .short
formatter.dateStyle = .short
formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let DOB = formatter.date(from: "2019/05/01 22:30")
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let createdDate = formatter.string(from: DOB!)
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.
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)