I want to display a date like this : weekday 7 month.
I did this and I have the correct syntax.
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE dd MMMM"];
[df setTimeZone:[NSTimeZone localTimeZone]];
The result logged is : Saturday 16 March. I just want to have the french date, not the english one. I try to set the local time zone but it changes nothing. Do you have an idea of how to do this ?
initialize a locale using an identifer.
you can get all available identifiers from
[NSLocale availableLocaleIdentifiers];
i believe #"fr_BI" is one of the french identifiers
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_BI"];
[dateFormatter setLocale:locale];
You set the locale of the date formatter using the appropriate NSLocale object.
Also, be wary of hard-coded date formats like that. Your users may not like seeing dates in that format, so you should run the format string through +[NSDateFormatter dateFormatFromTemplate:options:locale:] first. The WWDC 2012 session "Internationalization Tips & Tricks" had a bunch of useful information about this.
Related
In my app , Im using following code to convert string to date before inserting the date into the database.
However this code fails for the users in UK, they have the Region set to UK, and Timezone set to London.
This works for the users in the US as their locale is en_US. So that says, this code works fine for en_US locale but not en_GB locale.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T1'HH-mm-ss-SSS"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]]; //doing this as timestamp stored in server is based on UTC, hence I'm using UTC instead of systemTimeZone
date = [dateFormatter dateFromString:theDate];
The passed string is : 2014-6-26T121-21-6-000
If I set the locale as follows, instead of currentLocale for all the users across the world:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
then the code works, but I would like to know if this cause any issues in future?
Why we need set the locale property for converting the dates ?
Why the currentLocale fails in my case but not the en_US locale even though the date format is matched properly ?
Whenever you’re dealing with ISO 8601 or RFC 3339 dates (i.e. dates exchanged with web services and/or stored as a string in some data store) use en_US_POSIX. See Technical Note 1480.
Or one can use NSISO8601DateFormatter and you don’t have to deal with this locale silliness. E.g.
NSString *string = #"2014-06-26T12:21:06.000Z";
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
formatter.formatOptions = NSISO8601DateFormatWithInternetDateTime | NSISO8601DateFormatWithFractionalSeconds;
NSDate *date = [formatter dateFromString:string];
Also, standard representations of ISO 8601 and RFC 3339 datetime strings, you’d generally use a format like 2014-06-26T12:21:06.000Z where:
the hour is less than 24;
numbers are zero-padded;
separators between hours and minutes and seconds are :;
the separator between seconds and milliseconds is .; and
you'd often add Z at the end of the string to unambiguously designate that the time string is in GMT/UTC/Zulu.
This question already has answers here:
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 7 years ago.
I have problems with NSDate to NSString conversion, I have the following code in a method
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'zzz'"];
return [formatter stringFromDate:date];
If I´m not mistaken this should return a String like
"2016-01-15T22:45:40.GMT"
But the resulting string depends on the device configuration, meaning if the device shows the date as 24 hour then i get the result above, on the other hand if the device is configured to show the house as A.M. P.M. then I get
"2016-01-15T10:46:50 p.m..GMT"
From the documentation:
Although in principle a format string specifies a fixed format, by default NSDateFormatter still takes the user’s preferences (including the locale setting) into account. You must consider the following points when using format strings:
In iOS, the user can override the default AM/PM versus 24-hour time setting. This may cause NSDateFormatter to rewrite the format string you set.
I think you'll have to consider using your own formatting method if you want the result to have a fixed format, regardless of user settings.
Actually, from this answer (as pointed out by pbasdf), it appears that you can circumvent this by simply setting the locale:
NSLocale* en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocale:en_US_POSIX];
I have integrate RSS feed parser into iOS application. One of the field in the received data is published date. I'm able to parse that date if the iPhone locale is English-United States. But when I change the language of iPhone to Spanish, its not able to convert the string to NSDate object.
Here's the code that I wrote:
NSString* dt = #"Fri, 26 Jun 2015 00:00:00";
NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
//set the locale to spanish
[dtFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"es"]];
[dtFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss"];
NSDate* conDt = [dtFormatter dateFromString:dt ];
NSLog(#"%#", conDt); //This value is always (null)
Even after setting the locale to "es" (which is spanish), its still not able to parse it properly. How can I convert the string to date in iOS?
When you set the locale, you don't want to use the locale of the device, but rather the locale used when the string was created (because you're taking an English string and want to convert it to NSDate regardless of the locale of the device). In fact, it's advised to use en_US_POSIX:
[dtFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
See Technical Q&A #1480. This focuses on the Gregorian calendar issue with RFC 3999/ISO 8601 date strings, but it is applicable to language settings, too.
By the way, I notice that you're not setting the timezone. Often when dates do not bear any timezone information, they've been converted to GMT/UTC/Zulu. So you may want to set the timezone for your formatter, too:
[dtFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
Given that the time component of your string is "00:00:00", perhaps this isn't significant, but if dealing with datetime strings, you often want to make sure you correctly capture the timezone used within the string, as well.
So there is a section on web application that users can enter events into and the web service sends those events to the mobile app in the following format:
"yyyy-MM-dd'T'HH:mm:ssZZZZZ"
I'm having issues trying to convert the string into a date so I can get just the time from the event (formatted in the correct timezone as well), So for example here's one that comes over "2015-03-20T20:00:00-07:00", which when I pull the time should be 1PM Pacific Time. But instead I either get 8PM or 3AM (depending on whether I add UTC abbreviation to the date formatter).
Here's what I have so far, I know I'm missing something here & maybe there's another date formatter that needs to be used but so far I can't figure out where I'm going wrong.
NSString *datePattern = #"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:datePattern];
NSString *sString = [valueDict valueForKey:#"start_date"];
NSDate *startDate = [dateFormatter dateFromString:sString];
NSDateFormatter *timeFormatter = [NSDateFormatter new];
[timeFormatter setDateFormat:#"hh:mm a"];
[timeFormatter setLocale:[NSLocale systemLocale]];
NSString *timeString = [timeFormatter stringFromDate:startDate];
2015-03-20T20:00:00-07:00 is 8pm Pacific Daylight Time.
If you're representing 1pm PDT, that's either
2015-03-20T13:00:00-07:00
or represent that in "Zulu" (i.e. GMT/UTC)
2015-03-20T20:00:00Z
When working with a web service, the latter is the common convention for ISO 8601 dates. Then, when you present it to the user, you present it to them in their local timezone (using a NSDateFormatter with its default timeZone setting.
Note, when using NSDateFormatter to prepare ISO 8601 dates, you will want to ensure that you specify a locale of en_US_POSIX as outlined in Technical Q&A QA1480. When designing app for US audience this isn't critical, but it's best practice in case the user is not using a gregorian calendar on their device.
I have NSDate, and I need to convert it to string.
Usually I use this code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy"];
NSString *stringFromDate = [formatter stringFromDate:date];
return stringFromDate;
But I need to show data according to local date format. For instance, in USA the date usually starts from the month, but in Russia the date starts from day (#"dd/MM/yyyy"). And may be there is a different way of separators in different countries: '/', '.' ',' '-'
What is the best approach to do this?
Update (according to Jon Skeet answer):
if I specify NSDateFormatterMediumStyle the date is "4.9.2013", but for me it's better "04.09.2013". NSDateFormatterShortStyle doesn't help with this neither. Is there a way to add zeroes ?
See the date formatter guide which gives advice about this, suggesting that you set the style rather than calling setDateFormat:
NSDateFormatter makes it easy for you to format a date using the settings a user configured in the International preferences panel in System Preferences. The NSDateFormatter style constants—NSDateFormatterNoStyle, NSDateFormatterShortStyle, NSDateFormatterMediumStyle, NSDateFormatterLongStyle, and NSDateFormatterFullStyle—specify sets of attributes that determine how a date is displayed according to the user’s preferences.
It then gives a listing showing an example starting with this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
It sounds like you'd want NSDateFormatterShortStyle instead though.
setDateFormat is what you use for a custom format. More advice from the same document:
There are broadly speaking two situations in which you need to use custom formats:
For fixed format strings, like Internet dates.
For user-visible elements that don’t match any of the existing styles