Get a list of the last 10 Mondays

I want to have a drop-down menu that allows me to select "Week Starting on Monday 20th", returning 10 Mondays, but I'm not sure how to do this.

I used date.now (), etc., but don’t know how to do it.

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 
+4
source share
4 answers

You can find the formula for calculating the dates of previous Mondays, but why not do something simple:

  • Start a cycle.
  • Subtract one day.
  • Check it out, not Monday.
    • If so, add to the list.
    • If the list contains 10 items, an exit loop.
  • Return to the beginning of the cycle.

I am sure you can implement this. This is not the fastest way, but it is simple and probably fast enough for most purposes. If you want to optimize it, you can do it, but it is probably not worth your time if it is done many times per second.

+2
source

Let it work through him. I will do it in C #, but I hope some enterprising young polyglot can make a translation for me and hammer in the accepted answer.

DateTime.Today gets the date today. DateTime.Today.DayOfWeek gets you today "day of the week" as an enumeration, where Sunday is 0 and Saturday is 6.

So, we can get the last Monday using:

 var lastMonday = DateTime.Today.AddDays(1 - (int)DateTime.Today.DayOfWeek); 

edit There is one small glitch, in the event that today is Sunday, you will receive the date of tomorrow, and not last Monday. There is probably some really complicated math way around this, but it is just as easy to throw 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); 
+1
source

It may be late, but may be useful to others.

You can do what Mark said, but instead of continuing the cycle as soon as you find Monday, you can then subtract 7 (or add if you want to find the next 10 months) and get another Monday, and you can do it 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. 

It can save some time.

0
source

I was looking for something similar, just what I wanted to show the upcoming Mondays. This thread helped in some way. Thank you Despite the fact that this branch is outdated, for any of them it can be useful:

 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 
0
source

Source: https://habr.com/ru/post/1307607/


All Articles