views:

2832

answers:

4

what i am trying to do is to get NSDate today, yesterday, this Week, last Week, this Month, last Month variables ready for comparison for headers to be added on UITableView's titleForHeaderInSection

what i want is done manually in the code below for date 2009-12-11

 NSDate *today = [NSDate dateWithString:@"2009-12-11 00:00:00 +0000"];
 NSDate *yesterday = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];
 NSDate *thisWeek = [NSDate dateWithString:@"2009-12-06 00:00:00 +0000"];
 NSDate *lastWeek = [NSDate dateWithString:@"2009-11-30 00:00:00 +0000"];
 NSDate *thisMonth = [NSDate dateWithString:@"2009-12-01 00:00:00 +0000"];
 NSDate *lastMonth = [NSDate dateWithString:@"2009-11-01 00:00:00 +0000"];
A: 

Adapted from the Date and Time Programming Guide:

// Right now, you can remove the seconds into the day if you want
NSDate *today = [NSDate date];

// All intervals taken from Google
NSDate *yesterday = [today addTimeInterval: -86400.0];
NSDate *thisWeek  = [today addTimeInterval: -604800.0];
NSDate *lastWeek  = [today addTimeInterval: -1209600.0];
NSDate *thisMonth = [today addTimeInterval: -2629743.83]; // Use NSCalendar for
NSDate *lastMonth = [today addTimeInterval: -5259487.66]; // exact # of days

If you want the correct exact number of days depending on the month you should use an NSCalendar.

Ben S
sorry if you didnt understandyou are giving out last 7 days on thisWeekwhich in a way of saying is correct but i want actual weekthanks for NSCalendar suggestion
Hasnat
Yes, if you want the actual week starting on Saturday, then you'll need to use `NSCalendar`.
Ben S
Fixed constants for time are BAD BAD BAD. What about leap days or leap seconds? Use NSDateComponents or you WILL suffer some very nasty and hard to debug issues when your date stamps start mismatching.
Kendall Helmstetter Gelner
It's really not that bad if you just want approximately the last week's worth of posts or whatever. I specified that the OP should use `NSCalendar` if exactness was required.
Ben S
+2  A: 

Might be a better way to write this but here what i came up on Ben's NSCalendar suggestion and working from there to NSDateComponents

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];

[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);

[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];

components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];

[components setDay:([components day] - ([components weekday] - 1))]; 
NSDate *thisWeek  = [cal dateFromComponents:components];

[components setDay:([components day] - 7)];
NSDate *lastWeek  = [cal dateFromComponents:components];

[components setDay:([components day] - ([components day] -1))]; 
NSDate *thisMonth = [cal dateFromComponents:components];

[components setMonth:([components month] - 1)]; 
NSDate *lastMonth = [cal dateFromComponents:components];

NSLog(@"today=%@",today);
NSLog(@"yesterday=%@",yesterday);
NSLog(@"thisWeek=%@",thisWeek);
NSLog(@"lastWeek=%@",lastWeek);
NSLog(@"thisMonth=%@",thisMonth);
NSLog(@"lastMonth=%@",lastMonth);
Hasnat
You're leaking memory all over with those `fromDate:[[NSDate alloc] init]` bits.
Shaggy Frog
A: 

I really like the THCalendarInfo object contained in this project:

http://github.com/jaredholdcroft/kcalendar

I can't quite find the original. Using this object you can move to a previous day, the start of a week, the start of a month, get the day of a week, the day of a month... etc. etc.

Kendall Helmstetter Gelner
A: 

NSDateComponents is nice to get today:

NSCalendar *cal = [NSCalendar currentCalendar];

NSDate *date = [NSDate date];
NSDateComponents *comps = [cal components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) 
                                                          fromDate:date];
NSDate *today = [cal dateFromComponents:comps];

This creates a NSDate with only year, month and date:

(gdb) po today
2010-06-22 00:00:00 +0200

To get yesterday, etc. you can calculate it using NSDateComponents:

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:-1];
NSDate *yesterday = [cal dateByAddingComponents:components toDate:today options:0];
Christian