Formatting a date in Russian in Swift - 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.

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

how to change date format 2021-30-06T05:00:00+07:00 in swift?

i want to change date format from 2021-30-06T05:00:00+07:00 to get only time with format hh:mm:ss, but i didn't get it.
here is my code i already try
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "GMT+7")
let date = dateFormatter.dateFromString("2021-30-06T05:00:00+07:00")
// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
let timeStamp = dateFormatter.stringFromDate(date!)
print(timeStamp)
Your 2021-30-06T05:00:00+07:00 is not matching with yyyy-MM-dd'T'HH:mm:ss.
Date format should be yyyy-dd-MM'T'HH:mm:ssZZZZZ.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-dd-MM'T'HH:mm:ssZZZZZ"
let date = dateFormatter.date(from: "2021-30-06T05:00:00+07:00")
// change to a readable time format and change to local time zone
dateFormatter.dateFormat = "HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
let timeStamp = dateFormatter.string(from: date!)
print(timeStamp)
Output:
03:30:00 // This is as per my current timezone.
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
dateFormatter.locale = Locale.current
print(dateFormatter.string(from: post.createdDate))
Use DateFormatter instead of NSDateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-dd-MM'T'HH:mm:ssZZZZZ"
let yourDate = dateFormatter.date(from: "2021-30-06T05:00:00+07:00")
// change to local time zone from your format
dateFormatter.dateFormat = "dd MMMM yyyy HH:mm:ss"
dateFormatter.timeZone = TimeZone.current
let DateString = dateFormatter.string(from: yourDate!)
print(DateString)

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"

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