Dynamically determine the dates of Monday - Friday based on the current day

Hello Everyone,

I need to have an interface display the dates for Monday - Friday of the current week.  So for
example if the user pulls up the site on Tuesday June 27th, I need a piece of code to tell me the
date range of: Mon Jun 26th, 2006 - Fri Jun 30th, 2006

I'll be using it in a SQL query to pull up info in a datagrid.  (WHERE dates between ...)
I'm hoping for an end result that looks like:

                    Mon Jun 26th   | Tues Jun 27th  |  Wed Jun 28th  | Thu Jun 29th  |  Fri Jun 30th
Stat 1                 21%                 45%                    30%                  35%                45%
Stat 2                 25%                 35%                     40%                 55%                 27%

So regardless of when the user opens the page, it needs to know the date of that Monday & Friday.

Thanks!
Maurice
LVL 2
GRCHELPDESKAsked:
Who is Participating?
 
TornadoVConnect With a Mentor Commented:
No problem, here is the same code in VB.NET:

Dim mondayDate As DateTime = DateTime.MinValue
Dim fridayDate As DateTime = DateTime.MinValue
Select DateTime.Today.DayOfWeek
Case DayOfWeek.Monday
 mondayDate = DateTime.Today
 fridayDate = DateTime.Today.AddDays(4)
 ' break
Case DayOfWeek.Tuesday
 mondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(1))
 fridayDate = DateTime.Today.AddDays(3)
 ' break
End Select
0
 
TornadoVCommented:
Here is what you can do without using SQL query:

DateTime mondayDate = DateTime.MinValue;
        DateTime fridayDate = DateTime.MinValue;

        switch (DateTime.Today.DayOfWeek)
        {
            case DayOfWeek.Monday:
                mondayDate = DateTime.Today;
                fridayDate = DateTime.Today.AddDays(4);
                break;
            case DayOfWeek.Tuesday:
                mondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(1));
                fridayDate = DateTime.Today.AddDays(3);
                break;
        }

Just add the rest of the days of the week.
0
 
GRCHELPDESKAuthor Commented:
Hey TornadoV,

I think I see where you're going, but I don't know C very well.
Would you be able to post this in VB code please?

Thanks,
Maurice
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
GRCHELPDESKAuthor Commented:
Great, let me try to implement this.  Thanks for your help!
0
 
GRCHELPDESKAuthor Commented:
Hey TornadoV, it's working like a charm.  Thanks!

I'll include the finished code here so others can do a quick cut 'n paste.
TornadoV, if you notice any flaws let me know.  Thanks again...

        'Setup variables to contain Monday & Friday dates
        Dim MondayDate As DateTime = DateTime.MinValue
        Dim FridayDate As DateTime = DateTime.MinValue

        'Determine the Monday & Friday dates based on today's date
        Select Case DateTime.Today.DayOfWeek
            Case DayOfWeek.Monday
                MondayDate = DateTime.Today
                FridayDate = DateTime.Today.AddDays(4)
                ' break
            Case DayOfWeek.Tuesday
                MondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(1))
                FridayDate = DateTime.Today.AddDays(3)
                ' break
            Case DayOfWeek.Wednesday
                MondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(2))
                FridayDate = DateTime.Today.AddDays(2)
                ' break
            Case DayOfWeek.Thursday
                MondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(3))
                FridayDate = DateTime.Today.AddDays(1)
                ' break
            Case DayOfWeek.Friday
                MondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(4))
                FridayDate = DateTime.Today
                ' break
            Case DayOfWeek.Saturday
                MondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(5))
                FridayDate = DateTime.Today.Subtract(TimeSpan.FromDays(1))
                ' break
            Case DayOfWeek.Sunday
                MondayDate = DateTime.Today.Subtract(TimeSpan.FromDays(6))
                FridayDate = DateTime.Today.Subtract(TimeSpan.FromDays(2))
                ' break
        End Select

        'Display the results
        lblMonday.Text = MondayDate
        lblFriday.Text = FridayDate
0
 
TornadoVCommented:
Everything looks fine, just a small suggestion, you can safely remove 'break, I was simply translating from C# verbatim that is how it got into VB code.  
0
 
GRCHELPDESKAuthor Commented:
Ahh... I suppose the fact that it was commented out would have been my first clue.
I never even noticed, thanks for the heads up.  Thanks again!
0
All Courses

From novice to tech pro — start learning today.