I use this code to process a date string coming in from a json feed:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle: NSDateFormatterLongStyle];
[formatter setFormatterBehavior: NSDateFormatterBehavior10_4];
[formatter setDateFormat: #"EEE, dd MMM yyyy HH:mm:ss +0000"];
so if I call
NSDate *date = [formatter dateFromString: #"Tue, 08 Sep 2009 19:21:27 +0000"];
I get back a usable date if my region format is United States or United Kingdoms, but if I set it to Germany it returns nil. I understand there are some differences in behaviors across different locales, but if I define a format shouldn't that correct for any inconsistencies?
Names like "Tue" and "Sep" are English. Other languages use different names.
If you want to be able to parse English dates independent of the device's region settings, set your DateFormatter's locale to en_US using the -setLocale: method.
Thanks fixed it up with:
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"US"] autorelease]];
Related
I want to make date by date formatter
2012-07-12 but it display like
2012-07-11
My code:
NString * today_selected=[NSString stringWithFormat:#"%d%#%d%#%d",year_for_activated,#"-",month_for_activated,#"-",taged]; NSDateFormatter *Df=[[NSDateFormatter alloc]init];
//here year_of_=2012 and month_of_ac=7, and tag=12
but it display 2012-07-11 instead of 12.
[Df setDateFormat:#"YYYY-MM-DD" ];
NSDate *date_selected=[Df dateFromString: today_selected];
NSLog(#"today_selected:%#",date_selected);
but it display 2012-01-12
Please read the documentation which states
It uses yyyy to specify the year component. A common mistake is to use
YYYY. yyyy specifies the calendar year whereas YYYY specifies the year
(of "Week of Year"), used in the ISO year-week calendar. In most
cases, yyyy and YYYY yield the same number, however they may be
different. Typically you should use the calendar year.
Also you will note that the day is dd, NOT DD
When you find a problem like this, your first stop should be the documentation
try this:
NString *today_selected=#"2012-07-12";
NSDateFormatter *Df=[[NSDateFormatter alloc]init];
[Df setDateFormat:#"yyyy-MM-dd" ];
NSDate *date_selected=[Df dateFromString: today_selected];
NSLog(#"today_selected:%#",date_selected);
try this for get the 2012-07-12 ,this type of Formatter :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
How can I get my NSDate to display in the format for example i.e "Tue Feb 26, 2011"
Do it right. Don't hardcode your date formats. There are countries that are not your country and they might have different date formats. So if you want to show this date to the user you should use a method that takes the users locale into account.
You could use the dateFormatFromTemplate:options:locale: method introduced in iOS4 to get the appropriate format with all the information you want.
And if you have to support iOS < 4 you should create a plist with this template method to create the correct date format for the user locale.
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy" options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
NSLog(#"Formatted date: %#", [formatter stringFromDate:myDate]);
gives So., 27. Feb 2011 for my locale.
and Sun, Feb 27, 2011 for the en_US locale
NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc] init];
[gmtFormatter setDateFormat:#"E MMM d yyyy"];
I have implemented one iphone application in which I want to convert NSDate to NSString but in german format.
Can you give me some idea about that.
I am using below code.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[eventInfo valueForKey:#"startdat"] intValue]];
//2011-05-01 21:04:00 +0000(I am geeting this date)
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
NSLocale *nl_NL = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
[formatter1 setDateFormat:#"E,dd MMM yyyy"];
[formatter1 setLocale:nl_NL];
NSString *stringFromDate1 = [formatter1 stringFromDate:date];
[formatter1 release];
[nl_NL release];
//I am getting stringFromDate1 = "Mo.,02 Mai 2011" value.(wrong output)
Please give me idea
Use for example
[formatter1 setDateStyle:NSDateFormatterLongStyle];
instead of setDateFormat:.
The problem is that the time zone is taken into account when the NSDateFormatter is formatting the date. If you want the NSDateFormatter to format the exact same date as the NSLog'd version, you need to explicitly set the time zone of the formatter.
[formatter1 setDateFormat:#"E,dd MMM yyyy"];
[formatter1 setLocale:nl_NL];
[formatter1 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
I am not sure (from your question), what is your expected output. But assuming that you are looking for the full day of week description, try this date format:
[dateFormatter setDateFormat:#"EEEE, dd MMM yyyy"];
It gives the output:
Sonntag, 01 Mai 2011 (using de_DE locale)
In general, the number of characters determine the size of date field:
eg. Input date = 2011-05-01 Sunday
1-character = 1-digit/character number or word (if number/word can't be 1 character long then abbreviation or fullname is displayed).
[dateFormatter setDateFormat:#"E, d M y"]; // Sun, 1 5 2011
2-character = 2-digit/character number or word (if number/word can't be 2 character long then abbreviation is displayed).
[dateFormatter setDateFormat:#"EE, dd MM yy"]; // Sun, 01 05 11
3-character = 3-digit/character number or word, or abbreviation (generally).
[dateFormatter setDateFormat:#"EEE, ddd MMM yyy"]; // Sun, 001 May 2011
4-character = full name (generally).
[dateFormatter setDateFormat:#"EEEE, dddd MMMM yyyy"]; // Sunday, 0001 May 2011
Here's the weird part though, if you specify 5 E's, you get an rather unexpected output:
[dateFormatter setDateFormat:#"EEEEE, ddddd MMMMM yyyyy"]; // S, 00001 M 2011
For date formatting, I find the the following reference table very useful.
http://www.unicode.org/reports/tr35/#Date_Field_Symbol_Table
Good luck
Here is my code :
NSString *_date = #"Tue, 23 Nov 2010 16:14:14 +0000";
NSDateFormatter *parser = [[NSDateFormatter alloc] init];
[parser setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss '+0000'"];
[parser setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *date = [parser dateFromString:_date];
This doesn't run : 'date' is set to 'nil'. I tried with
[parser setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss ZZZ"];
With no more success...
Do you have any idea ?
Thanks in advance
Add this line:
NSDateFormatter *parser = [[NSDateFormatter alloc] init];
[parser setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
and it will work. By default, NSDateFormatter uses the system's current locale, which can vary depending on the current user's preferences. The date string above (#"Tue, 23 Nov 2010 16:14:14 +0000") contains English words ("Tue", "Sep") that would potentially not be recognized by the date formatter if the locale would be set to anything other than English.
Moreover, users from non-western cultures might use locales that use a different calendar than the Gregorian calendar that's used in the western world. If you did not explicitly set the locale, the date formatter might be able to parse the date but the resulting NSDate would represent a whole other point in time.
The locale identifier #"en_US_POSIX" is meant for this purpose. It is guaranteed to not change even if the #"en_US" locale should someday change its default settings.
The timezone specifier is 'z', so your string should be:
[parser setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
February, 26 2010 21:34:00
Based on all the documentation I can find, MMMM, d yyyy H:m:s should be correct - but my NSDate dateFromString is returning null.
I once had lots of trouble with exactly this kind of problem until I explicitly set the formatter to en_US locale. systemLocale is NOT good because it may be something other than en_US, affecting e.g month/weekday names etc. This is a piece of working code:
NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];
[fmt setDateFormat:#"eee MMM dd HH:mm:ss Z yyyy"];
[fmt setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
NSDate *formattedDate = [fmt dateFromString:someStringContainingDate];
What region is the phone set to? If it's not en_US, you'll have to set the date formatter to it using setLocale.