I wanna have a drop down that lets me sel开发者_JS百科ect "Week Commencing Monday the 20th" going back 10 Mondays but I'm not sure how to go about doing this.
I've used date.now(), etc. before but not sure how to do this one.
Thanks, Billy
UPDATED CODE
Public Sub GetMondays()
Dim dtMondays As New DataTable()
dtMondays.Columns.Add("Date")
Dim i As Integer = 1
While (dtMondays.Rows.Count < 11)
Dim Day As DateTime = Today.AddDays(-i)
If Day.DayOfWeek = 1 Then
dtMondays.Rows.Add(Day)
End If
i += 1
End While
drpDate.DataSource = dtMondays
drpDate.DataBind()
End Sub
You could come up with a formula for calculating the dates of the previous Mondays, but why not do something simple:
- Begin loop.
- Subtract one day.
- Check if it is Monday.
- If so, add to a list.
- If the list contains 10 elements, exit loop.
- Go back to start of loop.
I'm sure you can implement that. It's not that fastest way, but it's simple and probably fast enough for most purposes. If you want to optimize it, you can do so, but it's probably not worth your time unless this is being executed many times per second.
Let's work through it. I'll do this in C# but hopefully some enterprising young polyglot can do the translation for me and score the accepted answer.
DateTime.Today
gets you today's date. DateTime.Today.DayOfWeek
gets you today's "day of the week" as an enum, where Sunday is 0 and Saturday is 6.
So we can get the latest Monday using:
var lastMonday = DateTime.Today.AddDays(1 - (int)DateTime.Today.DayOfWeek);
edit There's one little glitch here, in that if today is Sunday you'll be getting tomorrow's date, not last Monday's. There's probably some really tricky mathematical way to get around that, but it's just as easy to throw in an extra check:
if (lastMonday > DateTime.Today) lastMonday = lastMonday.AddDays(-7);
Then we just need to get ten of them:
var lastTenMondays = from i in Enumerable.Range(0, 10)
select lastMonday.AddDays(i * -7);
I was searching for something similar, only that I wanted to display the upcoming Mondays. This thread helped in some way. Thanks! Even though this thread is old, for any one coming across, this may be helpful:
Public Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim nextMondays = DateTime.Today.AddDays(1 - CInt(DateTime.Today.DayOfWeek))
Dim i As Integer = 1
If nextMondays < DateTime.Today Then
nextMondays = nextMondays.AddDays(+7)
End If
For i = 0 To 14
ComboBox1.Items.Add("Mon. " + nextMondays.AddDays(i * +7))
Next i
End Sub
It's probably late but might be helpful for others.
You can do what Mark said but instead of continuing the loop once you find a Monday you can then subtract 7 (or add if you want to find the next 10 mondays) days and get the other monday and you can do this 10 times
* Begin loop.
* Subtract one day.
* Check if it is Monday.
o If so, add to a list.
o (in a for i = 1 to 10 loop) add 7 days and add to list (end for loop)
* Go back to start of loop.
this might save up some time.
精彩评论