I get date and time from server in the format Thu, 16 Feb 2012 13:54:41 +0000
Here I want just Thu, 16 Feb 2012 string from whole string . Can anyone tell me hoe i can take out this string.
In order of personal preference . . .
a) I'd parse it (using NSDateFormatter) into an NSDate object. They you can do whatever you want with it.
b) If you really only want the first part of the string, look into regular expressions.
c) If you're very confident about the formatting of the string, you could split it by the ' ' character and then join the first 3 substrings together :
NSArray *sub = [myDateString componentsSeperatedByString:@" "];
NSString *newDateString = [NSString stringWithFormat:@"%@ %@ %@",
[sub objectAtIndex:0],
[sub objectAtIndex:1],
[sub objectAtIndex:2]];
Try this simple example
NSString *dateStr = @"Thu, 16 Feb 2012 13:54:41 +0000";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//NSDate *today = [NSDate date]
[dateFormat setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss z"];
//NSString *dateString = [dateFormat stringFromDate:today];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:@"EEE, dd MMM yyyy"];
NSString *newStr = [dateFormat stringFromDate:date];
NSLog(@"date: %@", newStr);
[dateFormat release];