iphone - 2 个 NSDate 之间的差异(不包括周末)?

标签 iphone cocoa nsdate

对于熟悉 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/

相关文章:

ios - 合并两个数组的每个元素并附加到一个数组

iphone - 如何将第二行 UIPickerView 保存到 NSUserDefaults?

objective-c - "property"是 Objective-C 中的某种 "instance variable"还是它们不同?

iphone - 访问 Controller 中 Interface Builder 中创建的 UI 元素?

iphone - 获取下一个日期和前一天的日期

ios - 使用 Swift 3 获取 iPhone 或 iPad 设备的 IP 地址

iphone - UILabel 不更新

cocoa - 为什么等效键可以与 escape 一起使用,但不能与 return 一起使用?

swift - 如何将日期 "MM-dd-yyyy hh:mm:ss a"转换成文本格式?

objective-c - 从字符串创建 NSDate