0

as a part of a project, i'm trying to create a Calendar in Asp.net purely from codebehind with C# I'm using a repeater and filling it content from the codebehind which is working fine. I can use the Datetime to get todays date month etc.

But when im trying to calculate the date of the previous and next days of the week, the code gets very cluttered, i'm wondering if there is a better way to do it.

Currently this is how i generate the dates for the specific days.

if (today == "Monday" || today.Equals("Monday"))
            {
             switch (days)
                {
                    case "0":
                        return DateTime.Today.AddDays(0).ToString("dd");
                    case "1":
                        return DateTime.Today.AddDays(1).ToString("dd");
                    case "2":
                        return DateTime.Today.AddDays(2).ToString("dd");
                    case "3":
                        return DateTime.Today.AddDays(3).ToString("dd");
                    case "4":
                        return DateTime.Today.AddDays(4).ToString("dd");
                    case "5":
                        return DateTime.Today.AddDays(5).ToString("dd");
                    case "6":
                        return DateTime.Today.AddDays(6).ToString("dd");
                    default:
                        return "error";
                }
            }

And then for Tuesday

else if (today == "Tuesday" || today.Equals("Tuesday"))
            {
                switch (days)
                {
                    case "0":
                        return DateTime.Today.AddDays(-1).ToString("dd");
                    case "1":
                        return DateTime.Today.AddDays(0).ToString("dd");
                    case "2":
                        return DateTime.Today.AddDays(1).ToString("dd");
                    case "3":
                        return DateTime.Today.AddDays(2).ToString("dd");
                    case "4":
                        return DateTime.Today.AddDays(3).ToString("dd");
                    case "5":
                        return DateTime.Today.AddDays(4).ToString("dd");
                    case "6":
                        return DateTime.Today.AddDays(5).ToString("dd");
                    default:
                        return "error";
                }
            }

So in the example of tuesday, the case"0" is Monday and therefore if it's tuesday i subtract 1 day from the current date to get the date of the day before. The code works perfectly fine, but i can't help thinking that there must be a better way

And i have to create this piece of code for everyday of the week and the only thing really changing is the integer inside the "AddDays()"

also note the reason for the switch, is that all the if statements is called within a for loop, hence the odd cases in the switch.

If anyone smarter than me have an easier way to accomplish this please feel free to let me know.

  • Best Regards Andreas Hald.
Andreas Hald
  • 215
  • 3
  • 13

1 Answers1

0

I didn't understand well the meaning of the days variable but I assume that is the number of days you want to move back or forward You can simply use the following code:

return DateTime.Today.AddDays(days + todayDateTime.DayOfWeek).ToString("dd");

Then you can control the next and previous day with the "days" variable, being 1 or -1. Does it make sense?

Bonomi
  • 2,541
  • 5
  • 39
  • 51
  • That would be correct yes, sorry if i didn't clarify it enough. the days variable is simply the counter variable in a for loop that i didn't include, the different cases in the switch is not the amount of days that need to be moved forward or backwards it's a representation of the day where the date is needed. I realise now that it's probably stupidly named – Andreas Hald Oct 08 '14 at 10:17
  • So in that case you can change my code to: days + (DayOfWeek.Monday - todayDateTime.DayOfWeek) //in case of days = 0 and Monday todayDateTime.DayOfWeek -> 1 that returns 0 + 1-1 = 0 days + (DayOfWeek.Monday - todayDateTime.DayOfWeek) //in case of days = 1 and Monday todayDateTime.DayOfWeek -> 1 that returns 1 + 1-1 = 1 days + (DayOfWeek.Monday - todayDateTime.DayOfWeek) //in case of days = 0 and Tuesday todayDateTime.DayOfWeek -> 2 that returns 0 + 1-2=-1 days + (DayOfWeek.Monday - todayDateTime.DayOfWeek) //in case of days = 1 and Tuesday todayDateTime.DayOfWeek -> 2 that returns 1 + 1-2=0 – Bonomi Oct 08 '14 at 10:38