Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 10 days before from current date..

Thanks in advance.

like image 399
Sat Avatar asked Apr 02 '26 23:04

Sat


2 Answers

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);
like image 128
Joe Avatar answered Apr 04 '26 13:04

Joe


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.

like image 36
superjessi Avatar answered Apr 04 '26 12:04

superjessi