开发者

Taking the current Time and subtracting 3 hours from it

开发者 https://www.devze.com 2023-01-04 13:03 出处:网络
was wondering on how 开发者_开发技巧to take the current Time, and subtracting 3 hours from it to be stored in NSString?NSDate *now = [NSDate date];

was wondering on how 开发者_开发技巧to take the current Time, and subtracting 3 hours from it to be stored in NSString?


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消