NSDateFormatter on ios 5 - any other way to use? - ios5

I have a code that was working until iOS 4.3.5, now in iOS 5 it's not working:
//Example of date I'm using: Mon, 31 Oct 2011 15:57:55 BRST
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss vvvv"];
[formatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
NSString *dateString = [TBXML textForElement:[TBXML childElementNamed:#"pubDate" parentElement:XML]];
NSDate *data = [formatter dateFromString:dateString];
[formatter release];
Now, data is coming (null). Does anybody know what I did wrong?
Regards!
EDIT:
I was looking in the file NSDateFormatter.h and I found:
// - (id)init; // designated initializer
init is commented, so I am not allowed to use [[NSDateFormatter alloc] init] right? Is there any other way to create ah instance of NSDateFormatter ?

You can init the NSDateFormatter with initWithDateFormat:allowNaturalLanguage:
NSDateFormat Class Reference
SO-Question
NSDateFormatter's init method is deprecated?
Hope that helps?

Try this format:
#"EEE, dd MMM yyyy HH:mm:ss vvvv"
The docs are here.
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns

So what does dateString look like before you do anything to it?
That would help a lot.

Related

iPhone NSDateFormatter US region style

I am working on writing iPhone app.
I want to get current date as following format.
Tur, 18 Apr 2013 09:20:47
I tried with following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"E, dd MMM yyyy HH:mm:ss"];
NSString *formattedDate = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
The problem is my iPhone region is not US, so I get following string.
木, 18 4月 2013 09:20:47
How can I resolve this?
Set up the locale property of date to get it in US format
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]
autorelease];
or
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
I have't use NSDateFormatter from a long time but i am proving some links -
For better understanding you can refer -
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html and
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
I hope, it might help you.

Parse Date from string

I am not able to parse the following string into date
2012-12-28T00:01:51Z ,
I need date in format 28 Dec 2012
Please help me .
Thanks in advance
Abhishek
Try this way:
NSString *dateString=#"2012-12-28T00:01:51Z";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:#"YYYY-MM-dd'T'hh:mm:ss'Z'"];
NSLog(#"=> time %#",[dateFormatter dateFromString:dateString]);
NSDate *formattedDate=[dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"dd MMM YYYY"];
NSLog(#"Formatted date : %#",[dateFormatter stringFromDate:formattedDate]);
Why dont u pass the string simply as it is and then convert it into any format u want ?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];

change date format in iPhone

I am trying this since last two hours and finaly left it out for you guys :P
I need to convert this String Mon, 14 May 2012, 12:00:55 +0200
into Date dd/mm/yyyy hh:ss format..
Any kind of help towards the goal will be really appreciated.
What I have tried
I have tried using NSDateFormatter but I am unable to figure out the exact format of the above date.
This [dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"]; is how I have tried and many other formats too
Eg:
NSString *finalDate = #"Tue, 29 May 2012, 14:24:56 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:finalDate];
Here the date alwasys comes as nil
The most important part of date formatting is often forgotten, tell the NSDateFormatter the input language:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"EEE, dd MMMM yyyy, HH:mm:ss Z"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *date = [dateFormatter dateFromString:#"Mon, 14 May 2012, 12:00:55 +0200"];
NSLog(#"date: %#", date);
I've checked the output: date: 2012-05-14 10:00:55 +0000
Be aware that the HH in the date formatter is for 24hr.
Now I would suggest not to use a fixed output scheme, but use one of the NSDateFormatterStyle:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
Which in american english will output: 5/14/12 12:00 PM
This code is valid for ARC if you are not using ARC add autorelease to the NSLocale and release the NSDateFormatter after you are done with it.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd/mm/yyyy hh:ss"];
NSString *formatterDate = [inputFormatter stringFromDate:inDate];
[inputFormatter release];
get inforamtion about all Date Formate
my Blog Link is http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html
and also small function is bellow.....
-(NSString *) dateInFormat:(NSString*) stringFormat {
char buffer[80];
const char *format = [stringFormat UTF8String];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, format, timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
////////Like.......
NSString *mydate = [self dateInFormat:#"%Y%m%d-%H%M%S"];
hope,this help you....

dateFromString returning wrongDate

I am converting an NSString like #"12:25 AM May 27 2011" into NSDate.But as I convert it using the following code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a MMM dd YYYY"];
NSDate *date = [[dateFormatter dateFromString:#"12:25 AM May 27 2011"] copy];
[dateFormatter release];
I get the wrong Date as 2010-12-25 19:25:00 +0000
Can anyone please help me around here
It's not a wrong date, the time is displayed GMT hance (+0000) if you set the correct timezone very thing will be oke.

NSDateFormatter Iphone

I want to convert this ,
NSString *result1=#"Mon, 28 Sep 2009 06:35:42 PDT";
to nsdate using NSDateFormatter in iphone....
Can anyone help me?
Thanks in advance.....
I believe you want this:
NSString* dateString = #"Mon, 28 Sep 2009 06:35:42 PDT";
NSDateFormatter* newFormatter = [[[NSDateFormatter alloc] init] autorelease];
// Use this format to parse the string
[newFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss zzz"];
NSDate* aDate = [newFormatter dateFromString:dateString];
// Now change the format to that desired for printing
[newFormatter setDateFormat:#"MMM dd,yyyy HH:mm:ss aaa"];
NSLog(#"%#", [newFormatter stringFromDate:aDate]);
// Result:
// 2009-09-29 23:50:09.440 test[15396:903] Sep 28,2009 06:35:42 AM
You can find these codes here (as referenced in the NSDateFormatter documentation): http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
That's what I do in my program:
NSString *dateString = #"Mon, 28 Sep 2009 06:35:42 PDT";
NSDateFormatter *newFormatter = [[NSDateFormatter alloc] init];
[newFormatter setDateStyle:NSDateFormatterMediumStyle];
[newFormatter setTimeStyle:NSDateFormatterMediumStyle];
I'm using medium style, but there are more styles, you probably should use kCFDateFormatterLongStyle or kCFDateFormatterFullStyle, and then:
NSDate *aDate = [newFormatter dateFromString: dateString];
[newFormatter release];
Hope this helps