NSTimeInterval seems to be wrong for last day of the given month- iOS

↘锁芯ラ 提交于 2019-12-13 05:41:18

问题


In my local database, I have a list of NSTimeInterval values saved. I have to find out and fetch all records available in a given Month. The only problem is in fetching records for last day of the given month seems to be unavailable.

Lets say given month is December so I have to fetch all the records from 1st Dec to 31st Dec (Till 11:59PM)

I am using following implementation:

[self.dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *startDate = [self.dateFormatter dateFromString:@"1-12-2012"];
NSDate *endDate = [self.dateFormatter dateFromString:@"31-12-2012"];

NSTimeInterval startDateTimeInterval = [startDate timeIntervalSince1970];
NSTimeInterval endDateTimeInterval = [endDate timeIntervalSince1970];

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(mdate >= %f) AND (mdate <= %f)",startDateTimeInterval,endDateTimeInterval]];

I have noticed that endDateTimeInterval double value is pretty less as compared to saved value in the database for 31st (9AM). But howz it possible, I am expecting my endDateTime should be till 31st Dec 11:59 PM.

Please provide your inputs on this issue.


回答1:


The NSDate objects you are creating implicitly have a time of 00:00. So the predicate search will not include the last day from time 00:01 to 23:59. The easiest change is to set the end date to the next day (first of next month) and change the predicate to a less than, instead of less than or equal.

[self.dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *startDate = [self.dateFormatter dateFromString:@"1-12-2012"]; // start of range, inclusive
NSDate *endDate = [self.dateFormatter dateFromString:@"1-1-2013"]; // end of range, exclusive
...        
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"(mdate >= %f) AND (mdate < %f)",startDateTimeInterval,endDateTimeInterval]];



回答2:


Jason's answer will solve the problem but I think I've figured out the confusion: although in normal English a date usually specifies a whole day, in Cocoa terms an NSDate is an exact moment in time. So it has no length and a full description requires more detail than merely day/month/year. Most of the standard means of creating an NSDate offer precision to the nearest whole second, and obviously once you've converted to an 'NSTimeInterval` you can usually do better than that.

The NSLog is printing 30 because there are 30 whole days between the same time on the 1st of the month and the 31st, just like there's one whole day between the same time on the 1st and the 2nd, and zero whole days between the same time on the 1st and the 1st.

Jason's answer is therefore correct because it applies the time test of 'after the start of the first day of this month and before the start of the first day of the next month'. It's the 'the start of' bits that make the difference.



来源:https://stackoverflow.com/questions/13687433/nstimeinterval-seems-to-be-wrong-for-last-day-of-the-given-month-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!