Solved

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

Posted on 2006-06-27
7
648 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

23 Experts available now in Live!

Get 1:1 Help Now