iphone - How do you calculate the day of the year for a specific date in Objective-C? -
this found myself spending hours figure out , therefore want share you.
the question was: how determine day of year specific date?
e.g. january 15 15th day , december 31 365th day when it's not leap year.
try this:
nscalendar *gregorian = [[nscalendar alloc] initwithcalendaridentifier:nsgregoriancalendar]; nsuinteger dayofyear = [gregorian ordinalityofunit:nsdaycalendarunit inunit:nsyearcalendarunit fordate:[nsdate date]]; [gregorian release]; return dayofyear; where date date want determine day of year for. documentation on nscalendar.ordinalityofunit method here.
Comments
Post a Comment