Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-27
7
Medium Priority
?
658 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

927 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