I have a form with two DateTimePicker. the user chooses dates from the two DateTimePicker What i want to do is to show every Friday in between the two dates.
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListBox1.Items.Clear() Dim al As ArrayList al = DatesWithDay("01/01/2005", "01/01/2006", DayOfWeek.Friday)
For Each i In al ListBox1.Items.Add(i) Next End Sub
Function DatesWithDay(ByVal Date1 As Date, ByVal date2 As Date, ByVal dow As DayOfWeek) As ArrayList Dim d = DateDiff(DateInterval.Day, Date1, date2) Dim X As New ArrayList For d = 0 To d Dim checkdate As Date = Date1.AddDays(d)
If checkdate.DayOfWeek = DayOfWeek.Friday Then X.Add(checkdate) End If Next Return X End Function End Class
You took the code and hardcoded the controls etc. in there but your code is incorrect on the line
Displaying dates between two set dates
Gioking
The following should work - it will return an array list of dates between two dates for a given day of week.
Function DatesWithDay(ByVal Date1 As Date, ByVal date2 As Date, ByVal dow As DayOfWeek) As ArrayList
Dim d = DateDiff(DateInterval.Day, date1, date2)
Dim X As New ArrayList
For d = 0 To d
Dim checkdate As Date = date1.AddDays(d)
If checkdate.DayOfWeek = DayOfWeek.Friday Then
X.Add(checkdate)
End If
Next
Return X
End Function
Display Name*
JustinCase864
Sure the function I provided works just fine
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Clear()
Dim al As ArrayList
al = DatesWithDay("01/01/2005", "01/01/2006", DayOfWeek.Friday)
For Each i In al
ListBox1.Items.Add(i)
Next
End Sub
Function DatesWithDay(ByVal Date1 As Date, ByVal date2 As Date, ByVal dow As DayOfWeek) As ArrayList
Dim d = DateDiff(DateInterval.Day, Date1, date2)
Dim X As New ArrayList
For d = 0 To d
Dim checkdate As Date = Date1.AddDays(d)
If checkdate.DayOfWeek = DayOfWeek.Friday Then
X.Add(checkdate)
End If
Next
Return X
End Function
End Class
You took the code and hardcoded the controls etc. in there but your code is incorrect on the line
ListBox1.Items.Add(x.Add(checkdate))
This should be
ListBox1.Items.Add(CheckDate)
As your not using the arraylist in your code.
Patrick8
I used the codes and display the results in a list box and it came out as
0
1
2
3
4
5
6
Dim date1 As Date = dtp_from.Value
Dim date2 As Date = dtp_to.Value
Dim d = DateDiff(DateInterval.Day, date1, date2)
Dim x As New ArrayList
For d = 0 To d
Dim checkdate As Date = date1.AddDays(d)
If checkdate.DayOfWeek = DayOfWeek.Friday Then
ListBox1.Items.Add(x.Add(checkdate))
End If
Next
is this the right codes to list the results in a list box maybe i've writen the codes wrong :p
thanks