views:

81

answers:

1

I'm not sure if this is the best way to do this... but... I need to get the date/time since:

Midnight at the start of today
Midnight at the start of the current week (Sunday)
Midnight at the start of the current month
Midnight at the start of the current year

I use the NSDateFormatter:

[df setDateFormat:@"yyyy-MM-dd 00:00:00"]; strDate = [df stringFromDate:today]; 
[df setDateFormat:@"yyyy-MM-dd 00:00:00"]; strDate = [df stringFromDate:today];  // Sunday??? 
[df setDateFormat:@"yyyy-MM-01 00:00:00"]; strDate = [df stringFromDate:today]; 
[df setDateFormat:@"yyyy-01-01 00:00:00"]; strDate = [df stringFromDate:today]; 

They all seem to work EXCEPT "start of the current week (Sunday)".

How would I get that date? (Or is there a better way to do all of this?)

Thanks for everyone's help.

A: 

This might not be the best way, but I get the first day of the current week with this:

NSCalendar* cal = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents* comps = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
NSDate* date = [cal dateFromComponents:comps];
JoeGaggler
Did this work for you?
JoeGaggler