简体   繁体   中英

Asp.net Calendar - Generating dates

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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM