ios - Filter dates which are earlier than today? -
in application have list of nsdate instances. @ 1 point need seperate dates past today ie [nsdate date]
(earlier morning 12) , have used logic below. feel there must better logic this, me if there better solution.
i pass date instance argument method return bool value corresponding requirement.
-(bool)checkifpast:(nsdate *)date { nsdate *today=[nsdate date]; nsuinteger dateflags = nsyearcalendarunit|nsmonthcalendarunit|nsdaycalendarunit; nscalendar *gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *components1=[gregoriancalendar components:dateflags fromdate:today]; nsdatecomponents *components2=[gregoriancalendar components:dateflags fromdate:date]; if (components2.day-components1.day<0) { return yes; } else { return no; } }
bool checkiftodayorfuture(nsdate *date) { nsdate *now = [nsdate date]; nsuinteger dateflags = nscalendarunitera | nscalendarunityear | nscalendarunitmonth | nscalendarunitday; nscalendar *gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsdatecomponents *components = [gregoriancalendar components:dateflags fromdate:now]; nsdate *midnight = [gregoriancalendar datefromcomponents:components]; return [date compare:midnight] == nsordereddescending; }
if have make lot of tests keep midnight
around , compare:
on of dates.
keep in mind result not constant. example, user might change timezone interval of "today" changes.
Comments
Post a Comment