给定日期范围(开始日期和结束日期),我如何计算天数,不包括.Net中指定的星期几?

时间:2022-11-28 09:26:38

I'm creating a UI that allows the user the select a date range, and tick or un-tick the days of the week that apply within the date range.

我正在创建一个用户界面,允许用户选择日期范围,并勾选或取消勾选在日期范围内应用的星期几。

The date range controls are DateTimePickers, and the Days of the Week are CheckBoxes

日期范围控件是DateTimePickers,而星期几是CheckBoxes

Here's a mock-up of the UI:

这是UI的模型:

From Date: (dtpDateFrom)
To Date: (dtpDateTo)

从日期:(dtpDateFrom)到日期:(dtpDateTo)

[y] Monday, [n] Tuesday, [y] Wednesday, (etc)

[y]星期一,[n]星期二,[y]星期三,(等)

What's the best way to show a total count the number of days, based not only on the date range, but the ticked (or selected) days of the week?

什么是显示总计数天数的最佳方式,不仅基于日期范围,还基于一周的选定日期(或选定日期)?

Is looping through the date range my only option?

循环通过日期范围我唯一的选择?

2 个解决方案

#1


5  

Here's how I would approach it:

这是我如何接近它:

  • Find day of week (dow) of first and last date
  • 查找第一个和最后一个日期的星期几(dow)

  • Move first day forward to same dow as last. Store number of days moved that are to be included
  • 将第一天向前移动到与上次相同的道路。存储的移动天数

  • Calculate number of weeks between first and last
  • 计算第一个和最后一个之间的周数

  • Calculate number of included days in a week * number of weeks + included days moved
  • 计算一周中包含的天数*周数+包含的天数

As pseudo code:

作为伪代码:

 moved = start + end_dow - start_dow
 extras = count included days between start and moved
 weeks = ( end - moved ) / 7
 days = days_of_week_included * weeks + extras

This will take constant time, no matter how far apart the start and end days.

无论开始和结束日期相隔多远,这都需要一段时间。

The details of implementing this algorithm depend on what language and libraries you are using. Where possible, I use C++ plus boost::date_time for this sort of thing.

实现此算法的详细信息取决于您使用的语言和库。在可能的情况下,我使用C ++和boost :: date_time来做这种事情。

#2


2  

Looping through wouldn't be your only option - you could perform subtraction to figure out the total number of days, and subtract one for each of your "skipped" dates every week range in between that contains one of those days. By the time you figure out whether a day lands on one of the partial weeks at the beginning or end of the range and add #weeks * skipped days in a week, your code will be more complicated than it would be if just counted, but if you're expecting to have huge date ranges, it might perform better.

循环通过不是您唯一的选择 - 您可以执行减法以计算总天数,并为每个“跳过”日期减去一个,每个星期的范围包含其中一天。当你弄清楚一天是在某个星期开始或结束时的某个星期,并且在一周内添加#weeks *跳过的天数时,你的代码会比计算时更复杂,但是如果您期望拥有巨大的日期范围,它可能表现更好。

If I were you, I'd write the simple looping option and rewrite it if it turned out that profiling revealed it to be a bottleneck.

如果我是你,我会编写简单的循环选项并重写它,如果它发现分析显示它是一个瓶颈。

#1


5  

Here's how I would approach it:

这是我如何接近它:

  • Find day of week (dow) of first and last date
  • 查找第一个和最后一个日期的星期几(dow)

  • Move first day forward to same dow as last. Store number of days moved that are to be included
  • 将第一天向前移动到与上次相同的道路。存储的移动天数

  • Calculate number of weeks between first and last
  • 计算第一个和最后一个之间的周数

  • Calculate number of included days in a week * number of weeks + included days moved
  • 计算一周中包含的天数*周数+包含的天数

As pseudo code:

作为伪代码:

 moved = start + end_dow - start_dow
 extras = count included days between start and moved
 weeks = ( end - moved ) / 7
 days = days_of_week_included * weeks + extras

This will take constant time, no matter how far apart the start and end days.

无论开始和结束日期相隔多远,这都需要一段时间。

The details of implementing this algorithm depend on what language and libraries you are using. Where possible, I use C++ plus boost::date_time for this sort of thing.

实现此算法的详细信息取决于您使用的语言和库。在可能的情况下,我使用C ++和boost :: date_time来做这种事情。

#2


2  

Looping through wouldn't be your only option - you could perform subtraction to figure out the total number of days, and subtract one for each of your "skipped" dates every week range in between that contains one of those days. By the time you figure out whether a day lands on one of the partial weeks at the beginning or end of the range and add #weeks * skipped days in a week, your code will be more complicated than it would be if just counted, but if you're expecting to have huge date ranges, it might perform better.

循环通过不是您唯一的选择 - 您可以执行减法以计算总天数,并为每个“跳过”日期减去一个,每个星期的范围包含其中一天。当你弄清楚一天是在某个星期开始或结束时的某个星期,并且在一周内添加#weeks *跳过的天数时,你的代码会比计算时更复杂,但是如果您期望拥有巨大的日期范围,它可能表现更好。

If I were you, I'd write the simple looping option and rewrite it if it turned out that profiling revealed it to be a bottleneck.

如果我是你,我会编写简单的循环选项并重写它,如果它发现分析显示它是一个瓶颈。