Getting a localized date range string from two NSDates

I ran into a problem when it comes to two NSDate and displaying a localized string for a range.

For example, say my dates are July 7th and July 9th. I need the following localized strings:

  • RU: July 7-9
  • FR: 7-9 Juliet
  • ES: 7-9 Julio
  • DE: 7. bis. 9. Juli

Obviously using NSString stringWithFormat: will not work here. For the sake of simplicity, even if it doesn’t fall into the case when your range is two separate months. I know how to get a formatted string for each date, but it formats it in a range that bothers me.

Is there a way to use NSDateFormatter to get this? The more I look around, the more I think I need to have a switch for each locale.

EDIT: To clarify, I only need a date range for the user's locale. I do not need all of them at the same time.

+6
source share
2 answers

After further research, it looks like in iOS 8.0+, you can use NSDateIntervalFormatter for this. It does not help me now, but it is comforting to know this along the way.

+8
source

To develop Adam's answer with some sample code:

 let formatter = NSDateIntervalFormatter() formatter.dateStyle = NSDateIntervalFormatterStyle.ShortStyle formatter.timeStyle = NSDateIntervalFormatterStyle.NoStyle let dateRangeStr = formatter.stringFromDate(startDate, toDate: endDate) 
+5
source

Source: https://habr.com/ru/post/971894/


All Articles