1

I want to extract today, yesterday from date so it is localised automatically when I change language like which happens with weekdays and month.

Code for localised date that I am using:

let dateFormatter = Foundation.DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(identifier: "UTC")
let dateStr = dateFormatter.string(from: datePickerView.date)
let timeStr = "00:00:00.000"

let dateString = NSString(format:"%@ %@",dateStr,timeStr)
let mainDF = Foundation.DateFormatter()
mainDF.dateFormat = "MMMM dd, yyyy hh:mm:ss.SSS"
mainDF.locale = Locale(identifier: "en_US_POSIX")
mainDF.timeZone = TimeZone(identifier: "UTC")
let date = mainDF.date(from: dateString as String)

How to achieve this?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Amey
  • 795
  • 8
  • 31
  • 1
    Didn't get you. – dahiya_boy Dec 26 '17 at 07:13
  • This will help you: https://github.com/chrisamanse/iOS-NSDate-Utilities – Dharmesh Kheni Dec 26 '17 at 07:17
  • In I message Apple Make Today Section And When You Change the language its adopt to that language so how to make today , Yesterday Like that (Means Localised) ? Can i Extract Direct From date or any method to implement that . @dahiya_boy – Amey Dec 26 '17 at 07:25
  • @Amey Check #Uma answer, this is how you have to do. – dahiya_boy Dec 26 '17 at 07:38
  • what output you want ? Your question is not much clear! – Ketan Parmar Dec 26 '17 at 08:16
  • if i am displaying today , yesterday in my chat app when i change language to french today and yesterday should also be changed to french ? So how to achieve it . For ex if we get components from date like weekdays and months they change thier language . So how to get today , yesterday from date ? or any other method apple does this in i message app . @Lion – Amey Dec 26 '17 at 08:20
  • are you changing device's language? I mean from setting app are you changing language or you application have this functionality ? – Ketan Parmar Dec 26 '17 at 08:22
  • from Setting App I Am Changing Language – Amey Dec 26 '17 at 08:23

1 Answers1

4

DateFormatter has special flag for that: doesRelativeDateFormatting, which renders dates in relative format, using locale set for this formatter.

… If a date formatter uses relative date formatting, where possible it replaces the date component of its output with a phrase—such as “today” or “tomorrow”—that indicates a relative date. The available phrases depend on the locale for the date formatter; whereas, for dates in the future, English may only allow “tomorrow,” French may allow “the day after the day after tomorrow,” …

Example:

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.autoupdatingCurrent // Using system locale
dateFormatter.doesRelativeDateFormatting = true // Enabling relative date formatting

// other dataFormatter settings here, irrelevant for example
dateFormatter.timeStyle = .none
dateFormatter.dateStyle = .medium

let now = Date() 
let dateString: String = dateFormatter.string(from: now)  
print("dateString: \(dateString)") // Prints `dateString: <Today in current locale>`
user28434'mstep
  • 6,290
  • 2
  • 20
  • 35
  • So if i change language to spanish or any other will it support ? App Supports 10-11 Language so will it work or i have to write code to convert in every Locale ? – Amey Dec 26 '17 at 09:01
  • @Amey, it will use the locale set in your `DateFormatter`. Locales like [`.current`](https://developer.apple.com/documentation/foundation/locale/2293654-current) and [`.autoupdatingCurrent`](https://developer.apple.com/documentation/foundation/locale/2293741-autoupdatingcurrent) are using locale from system settings. So if you will use them it won't need to hardcode all your supported locales. – user28434'mstep Dec 26 '17 at 09:12
  • Can you Post Code It would be much Helpful @user28434 – Amey Dec 26 '17 at 09:14
  • var dateFormatter = DateFormatter() dateFormatter.timeStyle = .none dateFormatter.dateStyle = .medium dateFormatter.locale = Locale.autoupdatingCurrent dateFormatter.doesRelativeDateFormatting = true var date = Date(timeIntervalSinceNow: 0) var dateString: String = dateFormatter.string(from: date) print("dateString: \(dateString)") . This Code Works Fine Thanx A Lot – Amey Dec 26 '17 at 09:20