Rajendra
Rajendra

Reputation: 223

Not showing appropriate NSDate time in iphone?

I accessing a web service after i parsing that that code i got AiringTime of US EST 2011-10-17T14:00:00Z remaining code i written below

NSString *string = [[dateArray objectAtIndex:0] objectForKey:@"AiringTime"];
NSArray *stringArray1 = [string componentsSeparatedByString:@"T"];
NSString *string1 = [stringArray1 objectAtIndex:1];
NSArray *stringArray2 = [string1 componentsSeparatedByString:@"Z"];
NSString *dateInString = [stringArray2 objectAtIndex:0];

nowDate =[stringArray1 objectAtIndex:0];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"hh:mm:ss"];

NSDate *dateFromString =  [formatter dateFromString:dateInString];
[formatter release];

above NSDate *dateFromString given me appropriate output like 14:00 from morning to evening 6:30PM. After evening 6:30PM dateFromString gave mi nil value even though dateInString has an appropriate value?? Why? and How it happen?

I mean, NSDate *dateFromString = [formatter dateFromString:dateInString]; this line not working, dateFromString gave me nil output. I wasting my last 3 days evening time due to this strange problem. Please help me. Thanks in advance.

Upvotes: 1

Views: 170

Answers (1)

jrturton
jrturton

Reputation: 119242

See here, the example format is exactly the one you are dealing with.

Instead of splitting and manipulating the string yourself, simply do the following:

NSString *string = [[dateArray objectAtIndex:0] objectForKey:@"AiringTime"];
//Time in according to GMT+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateFromString =  [formatter dateFromString:string];
[formatter release];

If you are only interested in the time then NSDate is meaningless. What do you want to do with it afterwards?

Upvotes: 1

Related Questions