AutoFill Date Ranges base on selection

Posted on 2008-06-13
Last Modified: 2013-12-17
Dear Experts,

I  Have a DropDown with the following Lists Thisweek, Lastweek, ThisMonth, LastMonth and the textboxes  (Date1 & Date2).

What I want by selecting DropDown List it will also populate corresponding values as follows :

Selected:                       Date1                                   Date2
ThisWeek:                    Monday's Date                     Today' Date
ThisMonth                    1st Day of this Month           Today' Date
Lastweek                    Monday of Last week           Sunday of Last week
LstMonth                     1st Day of Last Month           End of Last Month


Question by:JaimeJegonia
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
  • 3

Accepted Solution

Pigtor earned 500 total points
ID: 21785633
Copy and paste the following code in the listBox1_SelectedIndexChanged event.

bool date1 = true;   //Indicates whether to retrieve the date1 or date2 value
                    int dayOfWeek = System.Convert.ToInt32(System.DateTime.Now.DayOfWeek);
                    //The start of the week must be on monday, for that reason we move one day back to the day of week variable.
                    if (dayOfWeek == 0) dayOfWeek = 6;  
                    else dayOfWeek -= 1;

                    DateTime selectedDate = DateTime.Now;  //This variable holds the calculated date.                  
                    DateTime today = System.DateTime.Now.Date;
                    switch (listBox1.SelectedIndex)
                        case 0:
                            if (date1) selectedDate = today.AddDays(dayOfWeek * -1);  //Monday's Date  
                            else selectedDate = today;  //Today's date
                        case 1:
                            if (date1) selectedDate = new DateTime(today.Year, today.Month, 1); //1st Day of this Month
                            else selectedDate = today;  //Today's date
                        case 2:
                            //Move back one week and calculate monday and sunday...
                            DateTime oneWeekBack = today.AddDays(-7);
                            if (date1) selectedDate = oneWeekBack.AddDays(dayOfWeek * -1); //Monday of Last week
                            else selectedDate = oneWeekBack.AddDays((6 - dayOfWeek)); //Sunday of Last week
                        case 3:
                            //Move back one month and calculate the first and last day...
                            DateTime oneMonthBack = today.AddMonths(-1);
                            if (date1) selectedDate = new DateTime(oneMonthBack.Year, oneMonthBack.Month, 1); //1st Day of Last Month
                            else selectedDate = new DateTime(oneMonthBack.Year, oneMonthBack.Month, DateTime.DaysInMonth(oneMonthBack.Year, oneMonthBack.Month)); //End of Last Month

I hope this is what you are looking for



Author Comment

ID: 21786768

Thank for answer, I'll try it.



Author Comment

ID: 21788308

Great!  It  needs only few tweaks to meet my requirement.



Author Closing Comment

ID: 31467149
Smart gist!

Featured Post

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…

690 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