By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,237 Members | 1,537 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,237 IT Pros & Developers. It's quick & easy.

How can I loop through each date in a given time period

P: 29
How can I loop through each date in a given time period so that I can add those dates as a column name in a table.
I want to create datatable with the person's name and hours he worked in a given time period. The user will select the dates and I want to show each day/date in that table as a column name. I have no idea how to do it. Please help.
Sep 2 '10 #1

✓ answered by Aimee Bailey

Usually you'd just iterate using the Days value of a TimeSpan, easy peasy :)

Expand|Select|Wrap|Line Numbers
  1. Public Function LoopThroughDates(ByVal dFrom As DateTime, ByVal dTo As DateTime) _
  2.                                         As List(Of DateTime)
  3.         Dim r As New List(Of DateTime)
  4.  
  5.         Dim diff As TimeSpan = dTo - dFrom
  6.  
  7.         For d = 0 To diff.Days
  8.             r.Add(dFrom.Date.AddDays(d))
  9.         Next
  10.  
  11.         Return r
  12.     End Function
  13.  

Share this Question
Share on Google+
1 Reply


Aimee Bailey
Expert 100+
P: 197
Usually you'd just iterate using the Days value of a TimeSpan, easy peasy :)

Expand|Select|Wrap|Line Numbers
  1. Public Function LoopThroughDates(ByVal dFrom As DateTime, ByVal dTo As DateTime) _
  2.                                         As List(Of DateTime)
  3.         Dim r As New List(Of DateTime)
  4.  
  5.         Dim diff As TimeSpan = dTo - dFrom
  6.  
  7.         For d = 0 To diff.Days
  8.             r.Add(dFrom.Date.AddDays(d))
  9.         Next
  10.  
  11.         Return r
  12.     End Function
  13.  
Sep 4 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.