NSDateFormatter returns a null date

Below is my code

NSString *dateandtime =[NSString stringWithFormat:@"%@",self.eventtime]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"]; NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [dateFormatter dateFromString:dateandtime]; 

If I print my name self.event, I get the output as Mon, 7 Jan 2013 15:30:00 CST , but when I pass this line to NSDateFormatter , I get null , I don’t know why I tried many combinations of date formats , but I can't decide if anyone can help me

+4
source share
4 answers

I solved my problem as Per Prateek, now its work for me ... Thanks Prateek.

 in your self.event, you are getting CST, it should be time zone for example +0530 
+1
source

Using:

 [dateFormatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"]; 
0
source

Use the method below to get NSDate from NSString date

 NSDate *dateFromString = [[NSDate alloc] init]; dateFromString = [self convertStringToDate:dateandtime]; 

Paste this method below into the .m file ...

 -(NSDate *)convertStringToDate:(NSString *) date { NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; NSDate *nowDate = [[[NSDate alloc] init] autorelease]; [formatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here format in which the date should be date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""]; nowDate = [formatter dateFromString:date]; // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate); return nowDate; } 

For more information on the NSDate component NSDate see this blog post on my NSDate-formate-info-for-iphone-sdk

0
source

Use this code ...

 - (NSString *)formattedDate:(NSString*)dateSte { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [NSTimeZone resetSystemTimeZone]; NSTimeZone *gmtZone = [NSTimeZone systemTimeZone]; [dateFormatter setTimeZone:gmtZone]; [dateFormatter setDateFormat:@"yyyy-MM-dd"]; NSDate *date = [dateFormatter dateFromString:dateSte]; [dateFormatter setDateFormat:@"EEE,MMM d,''yyyy"]; NSString *strDate = [dateFormatter stringFromDate:date]; return strDate; } 
0
source

All Articles