0

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.

2 Answers 2

1

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]];
Sign up to request clarification or add additional context in comments.

Comments

0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.