Search code examples
iosobjective-cnsdatensdateformatter

NSDateFormatter for day and month name


I am trying to get date in the format Monday, March 09, 2015. But following code is not returning required date format. I think I am using wrong Formatter. Here is the code:

NSString *dateString = @"09-03-2015";
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd MMMM yyyy"];
        NSDate *date = [[NSDate alloc] init];
        date = [dateFormatter dateFromString:dateString];
        NSLog(@"%@",[dateFormatter stringFromDate:date]);

Solution

  • try this...

    //Getting date from string
        NSString *dateString = @"09-03-2015";
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy"];
        NSDate *date = [[NSDate alloc] init];
        date = [dateFormatter dateFromString:dateString];
    // converting into our required date format    
        [dateFormatter setDateFormat:@"EEEE, MMMM dd, yyyy"];
        NSString *reqDateString = [dateFormatter stringFromDate:date];
        NSLog(@"date is %@", reqDateString);
    
    LOG:2015-03-09 12:40:33.456 TestCode [1377:38775] date is Monday, March 09, 2015