简体   繁体   English

Asp.net日历-生成日期

[英]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. 作为项目的一部分,我试图仅使用C#从背后的代码中在Asp.net中创建一个日历,我正在使用中继器并从其背后的代码中填充内容,这工作正常。 I can use the Datetime to get todays date month etc. 我可以使用Datetime获取今天的日期月份等。

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. 因此,在星期二的示例中,情况“ 0”是星期一,因此,如果是星期二,我将从当前日期减去1天以获得前一天的日期。 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()" 而且我必须为一周的每一天创建这段代码,唯一真正改变的是“ 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语句都在for循环内被调用,因此切换中的情况很奇怪。

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)。

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: 我不太了解days变量的含义,但我假设这是您要前进或后退的天数。您可以简单地使用以下代码:

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. 然后,您可以使用“天”变量(1或-1)控制第二天和前一天。 Does it make sense? 是否有意义?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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