-2

Am receiving date in this format (2012-07-13 09:22:22 +0000). I want to convert this date format to 13-Jul-2012 09:22. And also if the date is today means i want to change Today 09:22 and Yesterday 09:22. Can anyone please help to do this?

EDIT:

I have added some code what i have tried.

     NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
     [dateFormatter1 setDateFormat:@"dd-mm-yyyy hh:mm"]; 
     [dateFormatter1 setFormatterBehavior:NSDateFormatterBehaviorDefault];
     [dateFormatter1 setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
     NSDate *date = [dateFormatter1 dateFromString:dateStr];// Change Date Format

     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDoesRelativeDateFormatting:YES];
    NSString *dateString1 = [formatter stringFromDate:date]; // Getting Today or Tomorrow
    [formatter release];
    NSLog(@"DateString1 : %@", dateString1);
Gopinath
  • 5,192
  • 20
  • 62
  • 97

2 Answers2

1

First problem I see is that the date pattern your supplying to the Formatter does not match the date string you claim your getting. You might want to try this:

[dateFormatter1 setDateFormat:@"yyyy-mm-dd hh:mm:ss Z"];

The format your using in the code you supplied has year, month, day reversed and your not converting second.

Cliff Ribaudo
  • 8,509
  • 2
  • 50
  • 74
0

You can try this:

    NSDate *date = [NSDate date]; // date in this format 2012-07-14 17:32:09 +0000
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MMM-yyyy hh:mm"];
    //[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];  // Optional
    NSString *dateStr = [dateFormatter stringFromDate:date];

And next compare your 'date' with current date using NSDate methods for eg. timeIntervalSinceReferenceDate and then based on the result display today/tomorrow.

Rhythmic Fistman
  • 30,464
  • 5
  • 74
  • 138
Swathi
  • 99
  • 3