tags:

views:

63

answers:

2

Hi guys, was wondering on how to take the current Time, and subtracting 3 hours from it to be stored in NSString?

+4  A: 
NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setHour:-3];
NSDate *threeHoursAgo = [calendar dateByAddingComponents:comps toDate:now options:0];
[comps release];
[calendar release];

I leave the output as an NSString to you (use NSDateFormatter).

Ole Begemann
cool THanks! it works great!
Kenneth
+4  A: 

How about the [[NSDate alloc] initWithTimeIntervalSinceNow: -3*60*60] and using an NSDateFormatter?

king_nak
Cool. This works well too!
Kenneth