对于熟悉 Excel 的人来说,我正在尝试在 Cocoa 中使用类似的 NETWORKDAYS 函数。
任何人都可以帮助我构建一个 NSDate 类别所需的信息类型,该类别只能为我提供每周两个日期的工作日吗?
非常感谢 尼克
最佳答案
我知道我给出的并不是优化的,但这只是为了给你一种探索的方式。 您可以像这样使用 NSCalendar 和 NSDateComponents:
// Date of today
NSDate *today = [NSDate date];
// init the gregorian calendar
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Retrieve the NSDateComponents for the current date
NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
// Number of the day in the week (e.g 2 = Monday)
NSInteger weekday = [weekdayComponents weekday];
(参见 Calendars, Date Components, and Calendar Units )
从那里,您从第一个日期开始,每天重复此操作,直到结束日期,并使用工作日您可以确定这一天是否是周末。 (我重复一遍,这没有优化,但它只是一个轨道)
关于iphone - 2 个 NSDate 之间的差异(不包括周末)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2400712/