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

发布于 2024-08-25 00:40:52 字数 131 浏览 2 评论 0原文

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

任何人都可以帮助我构建一个 NSDate 类别所需的信息类型,该类别只能为我提供每周两个日期的工作日吗?

非常感谢 尼克

For those familiar with Excel, I'm trying to use a similiar NETWORKDAYS function within Cocoa.

Can anyone help with the type of info I'll need to construct an NSDate catagory that can give me only the working days betweek two dates?

many thanks
Nik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小忆控 2024-09-01 00:40:52

我知道我给出的并不是优化的,但这只是为了给你一种探索的方式。
您可以像这样使用 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];

请参阅 日历、日期组件和日历单位

从那里开始,您从第一个日期开始,每天迭代此操作,直到结束日期并使用您的工作日可以确定这一天是否是周末。 (我重复一遍,这没有优化,但它只是一个轨道)

I know that I'll give is not optimized, but it's just to give you a way to explore.
You can use the NSCalendar and the NSDateComponents like that:

// 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];

(see Calendars, Date Components, and Calendar Units)

From there you start from your first date and you iterate this for each day until your end date and using the weekday you can determine if the day is during a weekend or not. (I repeated that's not optimized but it's just a track)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文