Fetching time related data which includes current time
I have dat开发者_Python百科a stored where one of the entity's attributes is startTime
which is stores as an NSNumber (NSTimeInterval) and another attribute with the item's duration
.
How would I go about fetching the single item which is valid for "now" - i.e. teh item who's start time + duration includes the current time ([NSDate date])?
Have a look at this SO question: NSPredicate syntax for DATEADD?
You need to use an NSDateFormatter
to grab the current time/date and then use NSDateComponents
to break this into hours/mins/seconds (as required).
Then increment your startTime and duration onto it.
Then use the components to create a new NSDate
which will be your result.
精彩评论