gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:45:07 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com