ios - Get the first Monday in the month -
i test if today first monday of month. doing :
nscalendar *gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nscalendaridentifiergregorian]; nsdate *today = [nsdate date]; nsinteger weekdayofdate = [gregoriancalendar ordinalityofunit:nsweekdaycalendarunit inunit:nsweekcalendarunit fordate:today]; if (weekdayofdate == 2) { // monday }
but code correct every monday in month. how can first monday of month ?
you need add condition checking if we're in first 7 days of month. however, notice of values you're using have been deprecated, starting ios8. i've added complete version of code, including replacements deprecated values:
nscalendar *gregoriancalendar = [[nscalendar alloc] initwithcalendaridentifier:nscalendaridentifiergregorian]; nsdate *today = [nsdate date]; nsinteger weekdayofdate = [gregoriancalendar ordinalityofunit:nscalendarunitweekday inunit:nscalendarunitweekofyear fordate:today]; nsinteger dayofmonth = [gregoriancalendar ordinalityofunit:nscalendarunitday inunit:nscalendarunitmonth fordate:today]; if (weekdayofdate == 2 && dayofmonth <= 7) { // first monday of month }
Comments
Post a Comment