0

for example I have to choose in datetimepicker May 16,2014 the messge box will pop out "This Week" and if I choose in datetimepicker May 20,2014 it will pop out "Next Week" and also June 20,2014 will pop out "Next Month".

I tried this..

  System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
        DayOfWeek firstDayOfWeek = ci.DateTimeFormat.FirstDayOfWeek;
        int offset = firstDayOfWeek - DateTime.Now.DayOfWeek;
        DayOfWeek lastDayOfWeek = DateTime.Now.AddDays(offset).AddDays(6).DayOfWeek;
        DateTime nextmonth = DateTime.Now.AddMonths(1);



        DateTime input = DateTime.Now.AddDays(1);
        input = dateTimePicker1.Value;
        DateTime startOfWeek = DateTime.Today;
        while (startOfWeek.DayOfWeek != firstDayOfWeek)
            startOfWeek = startOfWeek.AddDays(-1);
        DateTime endOfWeek = DateTime.Now;
        while (endOfWeek.DayOfWeek != lastDayOfWeek)
            endOfWeek = endOfWeek.AddDays(1);

        bool thisWeek = input >= startOfWeek && input <= endOfWeek;
        bool Thismonth = input == startOfWeek && input < endOfWeek;
        bool nextMonth = input == nextmonth;

        if (thisWeek == true)
        {
            label1.Text = "This Week";
        }
        else if (thisWeek == false)
        {
            label1.Text = "Next Week";
        }
         else if (nextMonth == true) 
        {
            label1.Text = "Next Month";
        }
Never Stop Learning
  • 755
  • 1
  • 10
  • 34
  • Please consider to rewirte your post so it looks like a question. Right now it looks more as set of requirements... Also at the same time drop all "label" text from question and instead say "how to determine if given date falls in next week/next month". Likely all pieces of answer can be found in [determine if a given date is the Nth weekday of the month](http://stackoverflow.com/questions/288513/how-do-i-determine-if-a-given-date-is-the-nth-weekday-of-the-month) – Alexei Levenkov May 16 '14 at 02:43
  • (I've edited title - feel free to revert / improve the post). – Alexei Levenkov May 16 '14 at 02:48
  • for example sir I have to choose in datetimepicker May 16,2014 the messge box will pop out "This Week" and if I choose in datetimepicker May 20,2014 it will pop out "Next Week". – Never Stop Learning May 16 '14 at 02:50

1 Answers1

2

Not too much of a problem to do. C# provides lots of Date Time Functions, but not "Is this week" although you could write an extension method for this.

System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
DayOfWeek firstDayOfWeek = ci.DateTimeFormat.FirstDayOfWeek;
int offset = firstDayOfWeek - DateTime.Now.DayOfWeek;
DayOfWeek lastDayOfWeek = DateTime.Now.AddDays(offset).AddDays(6).DayOfWeek;

DateTime input = DateTime.Now.AddDays(1);
DateTime startOfWeek = DateTime.Today;
while (startOfWeek.DayOfWeek != firstDayOfWeek)
    startOfWeek = startOfWeek.AddDays(-1);
DateTime endOfWeek = DateTime.Now;
while (endOfWeek.DayOfWeek != lastDayOfWeek)
    endOfWeek = endOfWeek.AddDays(1);

Console.WriteLine("Week starts: " + startOfWeek);
Console.WriteLine("Week ends: " + endOfWeek);
Console.WriteLine("Input was: " + input);

Console.Write("Is input this week? ");
bool thisWeek = input >= startOfWeek && input <= endOfWeek;
Console.WriteLine(thisWeek);
TheNorthWes
  • 2,661
  • 19
  • 35
  • 1
    +0: Good start - Your answer needs work: use correct "first day of the week" property for current culture, remove random text (like "not too much of a problem). Please also use consistent coding standard for public code. Consider following default C# convention for variable names - `startOfWeek`. Also try not to mix `var`/explicit declaration in same small piece of code. – Alexei Levenkov May 16 '14 at 03:21
  • 1
    Thanks for the input @AlexeiLevenkov. I followed your advice and updated my answer to improve the answer. – TheNorthWes May 16 '14 at 03:36
  • Sir I got it. but how can I get Next Month or Next Year? – Never Stop Learning May 16 '14 at 04:02
  • For the same month check simply check if all parts of the date object are the same except for the day part. to determine if it next month simply check that current.Month == DateTime.Now.Month+1. Similar for year. [Here is a helpful question](http://stackoverflow.com/questions/4638993/difference-in-months-between-two-dates) – TheNorthWes May 16 '14 at 04:48
  • i tried this sir DateTime nextmonth = DateTime.Now.AddMonths(1); and I add bool nextMonth = input == nextmonth; but not a solution.. can u pls help? – Never Stop Learning May 16 '14 at 05:19
  • You are comparing the entire date time object. Please consider exploring the [DateTime](http://msdn.microsoft.com/en-us/library/system.datetime.aspx) documentation. input.Month == nextmonth.Month is what you need. – TheNorthWes May 16 '14 at 15:35