Thursday, 27 February 2014

NSDate few year ago from today. NSDate to future or past date.

    
    NSDate *today = [NSDate date];
    NSLog(@"today: %@", today);
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    [offsetComponents setYear:-18]; // setting it to -18 (near 18 years ago,from today, +18 will shift to future date)
//    [offsetComponents setMonth:-1];
//    [offsetComponents setDay:-1];
    
    NSDate *newShiftedDate = [gregorianCalendar dateByAddingComponents: offsetComponents toDate: today options:0];

    NSLog(@"newShiftedDate: %@", newShiftedDate);

No comments:

Post a Comment