What's the proper NSDateFormatter format for this string? - iphone

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.

Related

formate date as per my requirement

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 to get yyyy-mm-dd hh:mm:ss format from 28 Jul 2011 22:33:57 in iPhone?

I have stuck in issue in which i have to convert date format is Thu, 28 Jul 2011 22:33:57 +0000 into yyyy-mm-dd hh:mm:ss
please give me some idea how to do this?
What you want is to use the NSDateFormatter. Something like this:
NSDateFormatter* f = [[[NSDateFormatter alloc] init] autorelease];
[f setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString* dateString = [f stringFromDate:date];
Do note that the hours will still follow the users selected locale. Use kk:mm:ss to enforce a 24-hour time.
what have you tried? it's difficult to answer questions like this...
first you have to parse the date into an NSDate, use an NSDateFormatter, the incoming format looks like POSIX date format so should be easy.
then you want to output to the format you specify with another NSDateFormatter
You need to use an NSDateformatter to convert the first date to a string with the following syntax.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate: [NSDate date]]; //Put whatever date you want to convert
Then if you want the date as an NSDate and you have the string generated above just put the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm:ss"];
NSDate * date = [dateFormatter dateFromString: dateString]; //String generated above

NSDateFormatter dateFromString returns nil

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"];

dateformatter problem?

I want to change the date formats. But i don't know how to give the input format
`Thu, 2 Dec 2010 00:28:56 -0500' i use the date formatter for user custom format
NSString *inputString = #"Thu, 2 Dec 2010 00:28:56 -0500";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
NSDate *inputDate = [inputFormatter dateFromString:inputString];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"EEE. MMM. d, yyyy"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
mylabel.text = outputDate;
but date not display
please give me the solution
The format string you showed us before you edited your question was correct. The only thing that was missing was that you need to set the date formatter's locale to English if you want it to recognize English month and day names:
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];

NSDateFormatter with region format

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]];