DateFormatter and components order when using regex [duplicate] - ios

This question already has answers here:
Date formats from device based on locale
(5 answers)
Can you override NSDateFormatter 12 vs 24 hour time format without using a custom dateFormat
(1 answer)
Closed 5 years ago.
If I've understood correctly, if I want to customize how a date is displayed I have two options:
I can use regex such as "MM-dd-yyyy HH:mm"
I can set DateFormatter.Style to short, medium, etc.
In the second case I believe that the order of the components changes according to the specific country default. For example "06 Dec" automatically becomes "Dec 06" when switching to another language with that date default.
However that's not the case when using regex, correct? So this means that if I want to customize the date format with regex, I need to provide a specific regex for each country?

Related

Prevent dates from being converting when localizing in iOS

I am localizing an app. The app makes calls to an API that I do not have control over. When posting a request to the API, one of the expected values is a date string in the following format:
"Fri, 15 Mar 2019 15:53:14"
When switching application language to Spanish the date is applied to the date parameter as:
"vie, 15 mar 2019 15:48:42"
which makes sense. However, it causes the API to fail. If I had control over it, I would be passing the date in seconds, rather than a string, but unfortunately I do not own the API.
Does anyone know how to prevent dates from being converted when localizing?
Thanks!
On most the formatters, there is locale property which is the locale of the device by default. You can modify it :
dateFormatter.locale = Locale(identifier: "en_US")
Thanks to rmaddy
Prefer "en_US_POSIX" which is invariant in time.
if the US, at some point in the future, changes the way it formats
dates, "en_US" will change to reflect the new behaviour, but
"en_US_POSIX" will not)
https://developer.apple.com/library/archive/qa/qa1480/_index.html

localizing + customizing the date format of a UIDatePicker

I need to customize and localize the date format of a UIDatePicker's DateAndTime mode, so that it will display the date numbers in Arabic language plus time in 24 hours format and in Arabic numbers too.
the equivalent row in English would be: 2017 11 15 17:00
I've tried all the solutions proposed on SO from changing the locale to changing device language but to no avail.
Let's breaking it down:
For displaying the content of the date picker to be in Arabic language you'd need to set it your desired locale:
The locale used by the date picker.
For changing the format of the date picker, well... it depends on the used locale; As mentioned in the UIDatePicker - Internationalization:
Date pickers handle their own internationalization; the only thing you
need to do is specify the appropriate locale. You can choose a
specific locale for your date picker to appear in by setting the
Locale (locale) field in Attributes Inspector. This changes the
language that the date picker uses for display, but also the format of
the date and time (for example, certain locales present days before
month names, or prefer a 24-hour clock over a 12-hour clock)...
which means that setting the desired locale for changing the language also affects the format date of the date picker.
Roughly speaking, setting both language and format is unavailable for your case, you should choose the desired language or the desired date format.
However, applying the first point (changing the date picker locale):
datePicker.locale = Locale(identifier: "ar")
should leads to the following output:
Also, the following question:
Show Time in 12 and 24 hour format in UIDatePicker on the basis of app settings not on device Settings
is also related to changing the date picker format.

How do I convert a Time integer to a Date in Ruby? [duplicate]

This question already has answers here:
How to convert a unix timestamp (seconds since epoch) to Ruby DateTime?
(6 answers)
Closed 9 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I'm getting this stamp from an external API:
1391655852
and I need to convert it to a Date. I have seen a lot of conversion pages on the internet and on SO, but they all seem to be going the other way. I'm not familiar with this integer format at all.
Use Time.at for this:
t = Time.at(i)
I'll definitely yield to #duck's answer - in my opinion, that's the best way to convert an integer to Time - but if you're explicitly looking for a Date, you'll want to do a bit more work:
require 'date'
t = Time.at(i)
date = t.to_date
In the above example, date will be of class Date.

NSDateFormatter Can't format date [duplicate]

This question already has answers here:
Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?
(6 answers)
Closed 9 years ago.
I have tried and tried and researched and tried again but I cannot format this
The date is:
"2013-08-08T11:10:09-07:00"
I've tried using "yyyy'-'MM'-'DD'T'HH':'mm':'ssZ" and a host of different permutations of this but to no avail. I think perhaps the server is sending the incorrect format of the timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'DD'T'HH':'mm':'ssZ"];
return [dateFormatter dateFromString:[dictionary objectForKey:key]];
Any clues?
Thanks in advance
For the non-localized ISO offset that you're using—e.g., -07:00—you need to use 5 Z characters in your format. So, for the source data given, the correct format string would be:
yyyy-MM-dd'T'HH:mm:ssZZZZZ
While the currently used technical standard doesn't require date and time separators to be escaped, as Jeff mentions in the comments, it's probably not a bad idea to do so, especially if your source date is coming from a server or something. There are discussions on making those replacements, like the letters, that would change for locale-specific date and time separators. It also doesn't hurt to escape them from a technical perspective, it's just harder to read:
yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ
For more information, the current date formatter is based on the Unicode Technical Spec #35. The date and time pattern specifications for #35 can be found at http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns

Having trouble converting this NSString to NSDate [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?
I can't convert this NSString to an NSDate: 2010-03-06T10:06:00-05:00
To be honest, I can not decipher the Unicode docs, where they talk about generic wall time , standard/daylight time, offsets from GMT as a fallback, etc.
To show that I have made a decent effort, here is what I have as the dateformatter: yyyy-MM-dd'T'HH:mm:ssZZ:ZZ"
If I strip out the timezone such that the date becomes 2010-03-06T10:06:00 then I know what to do. But how many letter "Z"s do I need, and are they upper case? Should I be removing the colon from the -05:00 ? The timezone has been my achilles heel with these conversions.
Could someone please help me out?
Thank you!
Have a look at this ISO 8601 date formatter and see if that works for you.

Resources