我正在研究最好和最快的方法是在两个特定日期之间获得所有日子(周一、周二、周三)。
日期作为参数以逗号分隔的字符串传递给 Web 服务,例如mon,tue,wed,
目前这是伪代码。
所以我的(伪)代码如下
function Add(startdate, enddate, days)
{
dateList as New List(Of Date)
if days.split(',').count < 7 then
{
// This is the problem area.
}
while(startdate < enddate)
{
// do some processing of either the dates in dateList or just all days
}
}
这个问题对我来说是我
1)
- 将
days
参数拆分为数组, - 循环天数数组
- 每个
day
天数:- 经历和之间的每个
startdate
日期enddate
- 如果
date.day
是day
,则在此方法中添加到 dateList 可能有 (365 * 6) 检查(假设开始日期和结束日期相隔一年),如果选择了所有日期,我可以忽略将它们放入列表
- 经历和之间的每个
2)
- 经历和之间的每个
startdate
日期enddate
- 开关(日期.dayofweek.tostring())
- case 'mon': if days.contains('mon') then addDatetoList
3)
- 对于每个
everyday
之间startdate
和enddate
- 如果
days
.contains(everyday.dayofweek.tostring()) 然后 addDatetoList
4)有没有我忽略的另一种方法,所有这些都是完全低效的?
计算需要在 vb.net 代码隐藏中进行,否则我会考虑使用 sql server 来完成。
编辑
作为功能计时的练习,我快速构建了以下内容来尝试我的方法和其他方法。
Dim DaysOfWeek As String = "Monday,Tuesday,Friday"
Dim startDate As DateTime = DateTime.Now
Dim endDate As DateTime = DateTime.Now.AddDays(365)
Dim dateList As New List(Of DateTime)()
Dim method1StopWatch As New Stopwatch
Dim method2StopWatch As New Stopwatch
Dim method3StopWatch As New Stopwatch
Dim method4StopWatch As New Stopwatch
Dim executionStart As Long
Dim executionEnd As Long
Dim noDays As Integer = 0
'Method3
method3StopWatch.Start()
Do While (startDate < endDate)
If DaysOfWeek.Contains(startDate.DayOfWeek.ToString()) Then
dateList.Add(startDate)
noDays = noDays + 1
End If
startDate = startDate.AddDays(1)
Loop
method3StopWatch.Stop()
Label3.Text = method3StopWatch.ElapsedTicks.ToString() & " " & noDays & " Days"
noDays = 0
dateList.Clear()
startDate = DateTime.Now
'Method2
method2StopWatch.Start()
Do While (startDate < endDate)
Select startDate.DayOfWeek.ToString()
Case "Monday"
If DaysOfWeek.Contains("Monday") Then
dateList.Add(startDate)
noDays = noDays + 1
End If
Case "Tuesday"
If DaysOfWeek.Contains("Monday") Then
dateList.Add(startDate)
noDays = noDays + 1
End If
Case "Wednesday"
If DaysOfWeek.Contains("Wednesday") Then
dateList.Add(startDate)
noDays = noDays + 1
End If
Case "Thursday"
If DaysOfWeek.Contains("Thursday") Then
dateList.Add(startDate)
noDays = noDays + 1
End If
Case "Friday"
If DaysOfWeek.Contains("Friday") Then
dateList.Add(startDate)
noDays = noDays + 1
End If
Case "Saturday"
If DaysOfWeek.Contains("Saturday") Then
dateList.Add(startDate)
noDays = noDays + 1
End If
Case "Sunday"
If DaysOfWeek.Contains("Sunday") Then
dateList.Add(startDate)
noDays = noDays + 1
End If
End Select
startDate = startDate.AddDays(1)
Loop
method2StopWatch.Stop()
Label2.Text = (method2StopWatch.ElapsedTicks).ToString() & " " & noDays & " Days"
noDays = 0
dateList.Clear()
startDate = DateTime.Now
method1StopWatch.Start()
For Each Day As String In DaysOfWeek.Split(CChar(","))
Do While (startDate < endDate)
If startDate.DayOfWeek.ToString() = Day Then
noDays = noDays + 1
dateList.Add(startDate)
End If
startDate = startDate.AddDays(1)
Loop
startDate = DateTime.Now
Next
method1StopWatch.Stop()
Label1.Text = (method1StopWatch.ElapsedTicks).ToString() & " " & noDays & " Days"
noDays = 0
dateList.Clear()
startDate = DateTime.Now
method4StopWatch.Start()
Dim daysList As New List(Of DayOfWeek)()
daysList.Add(DayOfWeek.Monday)
daysList.Add(DayOfWeek.Tuesday)
daysList.Add(DayOfWeek.Friday)
Dim datesList As List(Of Date) = GetDayOfWeekDates(startDate, endDate, daysList)
method4StopWatch.Stop()
Label4.Text = (method4StopWatch.ElapsedTicks).ToString() & " " & datesList.Count & " Days"
End Sub
Public Function GetDayOfWeekDates(startDate As Date, endDate As Date, daysOfWeek As List(Of DayOfWeek)) As List(Of Date)
Dim liReturn As New List(Of Date)()
Dim currDay As Date = startDate
While currDay <= endDate
If daysOfWeek.Contains(currDay.Date.DayOfWeek) Then
liReturn.Add(currDay.Date)
End If
currDay = currDay.AddDays(1)
End While
Return liReturn
End Function
这导致周一、周二、周五的以下输出(经过的滴答声):
Method 1 : 10650 - 157 Days
Method 2 : 4152 - 157 Days
Method 3 : 4084 - 157 Days
Method 4 : 179 - 157 Days
这也意味着我的方法和逻辑很糟糕。