简体   繁体   中英

Dates in For Loop in vb.net

In vb.net I have two data values as shown below:

Dim startp as datetime
Dim endp as datetime

I have a function called ProcessData(soemdate) which processes dataporting.

In VB.net is there a way I can do something like

For each day between startp and endp
 ProcessData(soemdate)
Next 

Thanks

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
For Each Day As DateTime in Enumerable.Range(0, (endp - startp).Days) _
                            .Select(Function(i) startp.AddDays(i)) 
    ProcessData(Day)
Next Day

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.

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.

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.

You can easily loop through each day if you convert your dates to OLE Automation Date OADate where the left portion represents the day and the right portion represents the time.

For example #06/19/2018#.ToOADate converts to 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM