Search code examples
objective-cios5ios6nsdatexcode4.5

Convert date to next day's date in iOS


I have a date in string format just like 30-11-2012. I want the date next to it like 01-12-2012 again in string format. Is there any way of getting it?


Solution

  • I got the answer by someone I forget the name, I was about to accept his/her answer but when I clicked on accept answer it told me that the post bas been removed.

    The answer given by that anonymous person was given below

    NSString *dateString = @"22-11-2012";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSDate *dateFromString = [[NSDate alloc] init];
    dateFromString = [dateFormatter dateFromString:dateString];
    
    
    NSDateComponents *components= [[NSDateComponents alloc] init];
    [components setDay:1];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *dateIncremented= [calendar dateByAddingComponents:components toDate:dateFromString options:0];
    
    NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
    [myDateFormatter setDateFormat:@"dd-MM-yyyy"];
    NSString *stringFromDate = [myDateFormatter stringFromDate:dateIncremented];
    NSLog(@"%@", stringFromDate);
    

    In the end I really like to thanks that anonymous person, I know this was not as much tricky but this helped me a lot. Thanks again you the Anonymous Helper.