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"];
Related
One simple thing on conversion from NSString to NSDate. How can I convert Mon, 27 August 2012 01:30 AM to NSDate in this same format. I tried with NSDateFormatter. But I am not getting it in this required format. Can anyone help? This is what I tried.
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE,dd MM yyyy HH:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
NSDateFormatter is to specify the format that will appear in the date-string when extracting string from date or the format that is in the date-string when extracting date from string
So whenever you extract NSDate from a NSString, NSDate is always obtained in default date format(eg 2012-08-27 00:30:00 +0000)... only the when you extract NSString from NSDate, NSString can be obtained in desired(custom) format that you set in NSDateFormatter.
I hope this will help you sure!
NSDateFormatter *dateformater=[[[NSDateFormatter alloc] init]autorelease];
[dateformater setDateFormat:#"EEEE,dd MMMM yyyy HH:mm a"];
NSDate *todayTmp=[NSDate date];
NSString *conversionDate=[dateformater stringFromDate:todayTmp];
Note : (Upper case) HH for 24h time format, (Lower case) hh for 12h time format
NSString *myDateAsAStringValue = #"Mon, 27 August 2012 01:30 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSDate *myDate = [[NSDate alloc]init];
myDate = [df dateFromString:myDateAsAStringValue];
[df release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSString *strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
[dateFormatter release];
please use below code
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
your formatter is wrong
check this one
NSLog will return NSDate in a fixed format, i guess.
If we need Date in different format, we should have to format it via NSDateFormatter and get it as NSString.
Just a guess.
Don't forget to set the correct locale! If your device does not use an english locale NSDateFormatter can have problems to convert Mon and August into useful information because Mon is not the correct abbreviation for Monday in your language. For example in Germany the correct three letter abbreviation for Monday is Mon..
If you parse dates that have words in it you have to set the correct locale.
This should work:
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:posixLocale];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
EEE is the dateformatter code for a three letter weekday abbreviation.
hh is the dateformatter code for Hours between 1 and 12. HH means 0-23
MMMM is the full month, MM would be the numeric value (= 08) of the month.
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];
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]];
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.
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]];