Solved

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

Posted on 2006-06-27
7
644 Views
Last Modified: 2008-02-20
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
0
Comment
Question by:GRCHELPDESK
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:TornadoV
ID: 16997713
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
 
LVL 2

Author Comment

by:GRCHELPDESK
ID: 17002144
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
 
LVL 11

Accepted Solution

by:
TornadoV earned 500 total points
ID: 17002173
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:GRCHELPDESK
ID: 17002195
Great, let me try to implement this.  Thanks for your help!
0
 
LVL 2

Author Comment

by:GRCHELPDESK
ID: 17002572
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
 
LVL 11

Expert Comment

by:TornadoV
ID: 17002769
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
 
LVL 2

Author Comment

by:GRCHELPDESK
ID: 17003157
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now