Determine NSDate for Memorial Day in a given year

♀尐吖头ヾ 提交于 2019-12-06 07:35:05

To get the first Monday in a month, set weekdayOrdinal = 1 instead of weekOfMonth = 1:

NSInteger aGivenYear = 2012 ;

NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDateComponents* firstMondayInJuneComponents = [NSDateComponents new] ;
firstMondayInJuneComponents.month = 6 ;
firstMondayInJuneComponents.weekdayOrdinal = 1 ;
firstMondayInJuneComponents.weekday = 2 ; //Monday
firstMondayInJuneComponents.year = aGivenYear ;
NSDate* firstMondayInJune = [calendar dateFromComponents:firstMondayInJuneComponents] ;
// --> 2012-06-04

NSDateComponents* subtractAWeekComponents = [NSDateComponents new] ;
subtractAWeekComponents.week = -1 ;
NSDate* memorialDay = [calendar dateByAddingComponents:subtractAWeekComponents toDate:firstMondayInJune options:0] ;
// --> 2012-05-28

From the NSDateComponents documentation:

Weekday ordinal units represent the position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month.

What I'd probably do is get the day of the week of the first of the month (perhaps using NSDateFormatter), then calculate the date of the first Monday/Tuesday/Thursday/whatever of the month, then add (week# - 1) x 7 to that date to get the Nth Monday/whatever of the month.

For Memorial day you'd first check if the 5th Monday was still in May, if not use the 4th. (Or check going in, knowing that if Memorial day is on the 31st then the first Monday must be on the 3rd or earlier.)

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