I need to obtain the date of yesterday with NSDate object. Any idea?
EDIT : SOLVED:
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-86400];
Try this code. With this way, you can get next days, next months, previous days , previous months..
NSDate *now = [NSDate date];
int daysToAdd = -1;
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];
// create a calendar
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *yesterday = [gregorian dateByAddingComponents:components toDate:now options:0];
NSLog(@"Yesterday: %@", yesterday);
[gregorian release];
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(60.0f*60.0f*24.0f)];
Edit: This solution isn't correct. See Aadhiras answer for the correct solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With