相关疑难解决方法(0)

确定给定年份阵亡将士纪念日的 NSDate

有没有更好的方法来确定给定年份的阵亡将士纪念日(五月的最后一个星期一)的 NSDate ?

NSInteger aGivenYear = 2013 ;

NSCalendar* calendar = [NSCalendar currentCalendar] ;
NSDateComponents* firstMondayInJuneComponents = [NSDateComponents new] ;
firstMondayInJuneComponents.month = 6 ;
// Thanks, Martin R., for pointing out that `weekOfMonth` is wrong for returning the first Monday in June.
firstMondayInJuneComponents.weekOfMonth = 1 ;
firstMondayInJuneComponents.weekday = 2 ; //Monday
firstMondayInJuneComponents.year = aGivenYear ;
NSDate* firstMondayInJune = [calendar dateFromComponents:firstMondayInJuneComponents] ;

NSDateComponents* subtractAWeekComponents = [NSDateComponents new] ;
subtractAWeekComponents.week = 0 ;
NSDate* memorialDay = [calendar dateByAddingComponents:subtractAWeekComponents toDate:firstMondayInJune options:0] ;
Run Code Online (Sandbox Code Playgroud)

编辑 …

macos xcode cocoa nsdate nsdatecomponents

2
推荐指数
1
解决办法
940
查看次数

标签 统计

cocoa ×1

macos ×1

nsdate ×1

nsdatecomponents ×1

xcode ×1