【问题标题】:Difference between 2 NSDates, excluding Weekends?2 个 NSDate 之间的区别,不包括周末?
【发布时间】:2010-03-08 10:47:11
【问题描述】:

对于那些熟悉 Excel 的人,我正在尝试在 Cocoa 中使用类似的 NETWORKDAYS 函数。

谁能帮助我构建一个 NSDate 类别所需的信息类型,该类别只能给我两个日期之间的工作日?

非常感谢 尼克

【问题讨论】:

    标签: iphone cocoa nsdate


    【解决方案1】:

    我知道我给的不是优化的,但它只是给你一个探索的方式。 您可以像这样使用 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

    从那里你从你的第一个日期开始,你每天重复这个直到你的结束日期,并使用工作日你可以确定这一天是否在周末。 (我重复了那不是优化但它只是一个轨道)

    【讨论】:

    • 感谢亚尼克。如果有人知道比枚举每个 nsdate 实例来检查工作日值更有效的方法,那么我将不胜感激。
    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 2019-04-15
    相关资源
    最近更新 更多