问题
I have a core data context with objects that have a date stored in them. I would like to get the data for only the current week (Monday to Sunday) in that order. I am not looking for the last 7 days.
The date could be the one stored inside the entity or the data of when the object was added to core data (they are the same).
I can use a predicate with a start date and end date like this:
NSPredicate *weekPredicate = [NSPredicate predicateWithFormat:@"((date > %@) AND (date <= %@)) || (date = nil)",startDate,endDate];
but how do I calculate startDate and endDate?
回答1:
-(NSDate *) lastMondayBeforeDate:(NSDate*)timeStamp {
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps =
[gregorian components:NSWeekdayCalendarUnit fromDate:timeStamp];
NSInteger weekday = [comps weekday];
weekday = weekday==1 ? 6 : weekday-2; // start with 0 on Monday rather than 1 on Sunday
NSTimeInterval secondsSinceMondayMidnight =
(NSUInteger) [timeStamp timeIntervalSince1970] % 60*60*24 + weekday * 60*60*24;
return [timeStamp dateByAddingTimeInterval:-secondsSinceMondayMidnight];
}
-(NSDate *) nextMondayAfterDate:(NSDate*)timeStamp {
return [[self lastMondayBeforeDate:timeStamp]
dateByAddingTimeInterval:60*60*24*7];
}
回答2:
stating that you only need the current week starting from monday means that end date will be "today" and start date : "last monday".
Refering to Apple's documentation :
There are three main classes used for working with dates and times.
NSDate allows you to represent an absolute point in time.
NSCalendar allows you to represent a particular calendar, such as a Gregorian or Hebrew calendar. It provides the interface for most date-based calculations and allows you to convert between NSDate objects and NSDateComponents objects.
NSDateComponents allows you to represent the components of a particular date, such as hour, minute, day, year, and so on.
In addition to these classes, NSTimeZone allows you to represent a geopolitical region’s time zone information. It eases the task of working across different time zones and performing calculations that may be affected by daylight savings time transitions.
You'll then be able to look what day of the week we currently are :
NSDateComponents has this method for example :
- (NSInteger)weekday
Return Value The number of weekday units for the receiver.
Discussion Weekday units are the numbers 1 through n, where n is the number of days in the week. For example, in the Gregorian calendar, n is 7 and Sunday is represented by 1.
here is the link towards the date and time programming guide
回答3:
Just a bit of advice from someone that has worked with dates and times a lot in the past. Dates can get messy! There's no way around it. You wind up with many, many lines of code and DateCompnents flying everywhere not to mention Calendars and timezones just to calculate something you thought would just require a bit of math. You really should check out the infamous Erica Sadun's NSDate categories.(NSDate-Extentions).
https://github.com/erica/NSDate-Extensions
I can't tell how much I wished that I found this sooner. Also read the Date and Programming guide from the Apple Doc's, and when your done read it again, it's not light reading in the least.
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtDates.html
(I know, this is a comment not an answer.)
来源:https://stackoverflow.com/questions/9758813/core-data-get-data-from-current-week