vb.net 中 For 循环中的日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1335339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dates in For Loop in vb.net
提问by Joel Coehoorn
In vb.net I have two data values as shown below:
在 vb.net 中,我有两个数据值,如下所示:
Dim startp as datetime
Dim endp as datetime
I have a function called ProcessData(soemdate)
which processes dataporting.
我有一个称为ProcessData(soemdate)
处理数据移植的函数。
In VB.net is there a way I can do something like
在 VB.net 中有一种方法可以做类似的事情
For each day between startp and endp
ProcessData(soemdate)
Next
Thanks
谢谢
回答by JohnFx
Here is another way to do this.
这是执行此操作的另一种方法。
Dim startP As DateTime = New DateTime(2009, 1, 1)
Dim endP As DateTime = New DateTime(2009, 2, 1)
Dim CurrD As DateTime = startP
While (CurrD <= endP)
ProcessData(CurrD)
Console.WriteLine(CurrD.ToShortDateString)
CurrD = CurrD.AddDays(1)
End While
回答by Joel Coehoorn
For Each Day As DateTime in Enumerable.Range(0, (endp - startp).Days) _
.Select(Function(i) startp.AddDays(i))
ProcessData(Day)
Next Day
回答by Ryan McArthur
Adding to Joel Coehoorn's answer which I personally think should be the accepted answer as I always try to avoid While loops no matter how safe they may appear. For...Each is a much safer approach although the enumerable isn't very pretty in-line. You can however move it to a function to keep things more readable, plus you can re-use as needed.
添加到 Joel Coehoorn 的答案中,我个人认为这应该是公认的答案,因为无论它们看起来多么安全,我总是尽量避免 While 循环。For...Each 是一种更安全的方法,尽管 enumerable 不是很内联。但是,您可以将其移动到一个函数中以保持可读性,此外您还可以根据需要重新使用。
For Each Day As DateTime In DateRange(StartDate, EndDate)
ProcessData(Day)
Console.WriteLine(Day.ToShortDateString)
Next
Public Shared Function DateRange(Start As DateTime, Thru As DateTime) As IEnumerable(Of Date)
Return Enumerable.Range(0, (Thru.Date - Start.Date).Days + 1).Select(Function(i) Start.AddDays(i))
End Function
I also added 1 to Enumerable range since as Joel had it, it wouldn't return the end date and in my situation I needed it to return all dates in the range including the start and end days.
我还将 1 添加到 Enumerable 范围,因为 Joel 拥有它,它不会返回结束日期,在我的情况下,我需要它返回范围内的所有日期,包括开始和结束日期。
Enumerable.Range is a sort of loop in itself that adds i days to the startdate advancing i with each call from in this case 0 to the difference between start and end days + 1. So the first time it's called you get the result of Start.AddDays(0), next you'll get Start.AddDays(1) and so on until the range is complete.
Enumerable.Range 本身就是一种循环,它将 i 天添加到开始日期,在这种情况下,每次调用从 0 到开始和结束天之间的差 + 1。所以第一次调用它时,您会得到 Start 的结果.AddDays(0),接下来您将获得 Start.AddDays(1),依此类推,直到范围完成。
回答by GDavoli
You can easily loop through each day if you convert your dates to OLE Automation DateOADate
where the left portion represents the day and the right portion represents the time.
如果将日期转换为OLE 自动化日期OADate
,其中左侧部分表示日期,右侧部分表示时间,则您可以轻松地遍历每一天。
For example #06/19/2018#.ToOADate
converts to 43270.0
例如#06/19/2018#.ToOADate
转换为43270.0
For loopDate As Double = #06/19/2018#.ToOADate To #07/01/2018#.ToOADate
Dim thisDate As Date = DateTime.FromOADate(loopDate)
' Do your stuff here e.g. ProcessData(thisDate)
Next
回答by John Gietzen
Yes, you can use an accumulator date:
是的,您可以使用累加器日期:
Dim Accumulator as DateTime
Accumulator = startp
While (Accumulator <= endp)
Accumulator = Accumulator.AddDays(1)
End While
Not tested, and I'm a C# programmer, so be easy if my syntax is wrong.
未经测试,我是一名 C# 程序员,所以如果我的语法有误,请轻点。
回答by Carlos Galhano
Set a calendar table with all dates and query values from there.
设置一个包含所有日期和查询值的日历表。
SQL:
查询语句:
Select Date as MyDate from tblCalendar Where Date >= StartDt And Date <= EndDate
.NET:
。网:
While Reader.read
process(MyDate)
End While