Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-06-27
7
650 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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