A friend asked me yesterday if this was possible on the iPhone. I took a look at NSCalendar and all the related Classes but couldn't find a solution to this.
昨天有个朋友问我,这在iPhone上是否可行。我查看了NSCalendar和所有相关的类,但是没有找到解决方案。
So I thought about this approach: If I had two dates dateA and dateB, I would have to make a for-loop and iterate over every single day in this interval. Then I would count the business days monday until friday, and return the result.
所以我想到了这个方法:如果我有两个date dateA和dateB,那么我必须创建一个for循环,并在这个间隔中对每一天进行迭代。然后我将计算星期一到星期五的工作日,并返回结果。
Then I went to bed, and I woke up with this probably much better idea: I need to know what day is it when I start. Lets say it's thursday. And then I must know how many days are in that interval. The last part is not hard to figure out. For the first part, I have no clue yet, but I believe there's an day of week value in NSCalendar. With that, I could do some simple math to calculate the amount of business days.
然后我上床睡觉,醒来的时候我想到了一个更好的主意:我需要知道什么时候开始。让说这是星期四。然后我必须知道这个间隔有多少天。最后一部分不难理解。对于第一部分,我还不知道,但是我相信在NSCalendar有一个星期的价值。有了它,我可以做一些简单的计算来计算工作日的数量。
Did anyone do that already on the iPhone?
有人在iPhone上做过吗?
2 个解决方案
#1
2
How many times are you performing this calculation? Do the dates you'll be needing have to fall in a specific range?
你计算了多少次?你需要的日期必须在一个特定的范围内吗?
If you're performing lots of these business date differences, consider building a lookup table that will store the number of business days between that date and some arbitrary date. Then to find the number of business days between two dates just subtract those lookup values. As for day of week calculations... remember that not all weekdays are "business days", so you'd need to somehow account for holidays.
如果您正在执行许多这些业务日期差异,请考虑构建一个查找表,它将存储该日期与某个任意日期之间的业务天数。然后,要找到两个日期之间的业务天数,只需减去这些查找值。至于星期计算……记住,不是所有的工作日都是“工作日”,所以你需要考虑假期的原因。
#2
2
Here are the pieces with which to do this calculation:
下面是计算的部分:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Determine the integer number of weeks between the two dates
NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:firstDate toDate:secondDate options:0];
NSInteger numWeeks = [components week];
// Determine the day of the week for each date
components = [calendar components:NSWeekdayCalendarUnit fromDate:firstDate];
NSInteger firstWeekday = [components weekday];
components = [calendar components:NSWeekdayCalendarUnit fromDate:secondDate];
NSInteger secondWeekday = [components weekday];
[calendar release];
I leave it as an exercise for the reader to do the necessary math to work out the number of business days from these values.
我把它作为一个练习留给读者去做必要的计算,从这些值算出工作日的数量。
#1
2
How many times are you performing this calculation? Do the dates you'll be needing have to fall in a specific range?
你计算了多少次?你需要的日期必须在一个特定的范围内吗?
If you're performing lots of these business date differences, consider building a lookup table that will store the number of business days between that date and some arbitrary date. Then to find the number of business days between two dates just subtract those lookup values. As for day of week calculations... remember that not all weekdays are "business days", so you'd need to somehow account for holidays.
如果您正在执行许多这些业务日期差异,请考虑构建一个查找表,它将存储该日期与某个任意日期之间的业务天数。然后,要找到两个日期之间的业务天数,只需减去这些查找值。至于星期计算……记住,不是所有的工作日都是“工作日”,所以你需要考虑假期的原因。
#2
2
Here are the pieces with which to do this calculation:
下面是计算的部分:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
// Determine the integer number of weeks between the two dates
NSDateComponents *components = [calendar components:NSWeekCalendarUnit fromDate:firstDate toDate:secondDate options:0];
NSInteger numWeeks = [components week];
// Determine the day of the week for each date
components = [calendar components:NSWeekdayCalendarUnit fromDate:firstDate];
NSInteger firstWeekday = [components weekday];
components = [calendar components:NSWeekdayCalendarUnit fromDate:secondDate];
NSInteger secondWeekday = [components weekday];
[calendar release];
I leave it as an exercise for the reader to do the necessary math to work out the number of business days from these values.
我把它作为一个练习留给读者去做必要的计算,从这些值算出工作日的数量。