Date in CocoaTouch
I want to find the current date and the date that 10 days before from current date.I know how to find the current date.please anybody help me to find the date which 1开发者_如何学JAVA0 days before from current date..
Thanks in advance.
You can use NSDateComponents
to subtract the days from the current date.
NSDate *today = [NSDate date];
NSDateComponents *sub_date = [[NSDateComponents alloc] init];
[sub_date setDay:-10];
NSDate *tenDaysAgo = [[NSCalendar currentCalendar] dateByAddingComponents:sub_date
toDate:today
options:0];
[sub_date release];
NSLog(@"Ten Days Ago: %@", tenDaysAgo);
You can use dateWithTimeInterval:sinceDate:
to subtract 10 days from the current date.
Code example:
NSDate *todayDate = [NSDate date];
NSDate *tenDaysAgoDate = [NSDate dateWithTimeInterval:-864000 sinceDate:todayDate];
The 864000 represents the seconds in ten days, the negative sets the calculation to days AGO, instead of forward.
精彩评论