Recently a company A project has a pit, show no 0 points of data, modify the starting time of the day before 23 points 59 minutes 59 seconds can be displayed.
Check out the 0-point code for the day, and the results are basically consistent with those found online, as shown below (using category on NSDate):
1-(NSDate *) Zeroofdate2 {3Nscalendar *calendar =[Nscalendar Currentcalendar];4Nsdatecomponents *components =[Calendar Components:nsuintegermax fromdate:self];5Components.hour =0;6Components.minute =0;7Components.second =0;8 return[Calendar datefromcomponents:components];9}
Plus log found that the return of the date Nstimeinterval value after the decimal point is not 0, because nanosecond is not 0, and iOS can not use Components.nanosecond = 0, so use the following methods to solve:
1-(NSDate *) Zeroofdate2 {3Nscalendar *calendar =[Nscalendar Currentcalendar];4Nsdatecomponents *components =[Calendar Components:nsuintegermax fromdate:self];5Components.hour =0;6Components.minute =0;7Components.second =0;8 9 //Components.nanosecond = 0 Not available in IOSTenNstimeinterval ts = (Double)(int) [[Calendar datefromcomponents:components] timeIntervalSince1970]; One return[NSDate datewithtimeintervalsince1970:ts]; A}
Make a mark in case the pit is later.