简体   繁体   English

如何使用C#将6天添加到日期变量

[英]how to add 6 days to a date variable using C#

I have variable which holds a date value so I would like to add 6 days to the current date variable. 我有一个保存日期值的变量,所以我想在当前日期变量中添加6天。 The variable always changes based on user's selection but i always would like to add 6 days to the current date variable. 该变量始终根据用户的选择而更改,但是我始终想将6天添加到当前日期变量中。 Here is what i have and i would like to create a variable called WeekEndDate and this variable should hold the value of WeekBeginDate + 6 days. 这是我所拥有的,我想创建一个名为WeekEndDate的变量,该变量应包含WeekBeginDate + 6天的值。

protected void Page_Load(object sender, System.EventArgs e)
{

    DateTime WeekBeginDate;
    DateTime WeekEndDate;
    if(this.Page.Request["WeekBeginDate"] != null)
    {
        WeekBeginDate = DateTime.Parse(this.Page.Request["WeekBeginDate"].ToString());

        Chart1.Titles[0].Text = WeekBeginDate.ToString();
        WeekEndDate = WeekBeginDate.AddDays(6);


    }

    // load the chart with values

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString);
    string sqlStatement = "SELECT DATEPART(DAY,DT)DT, sum (HOURS) as HOURS FROM myTable where  DT >= WeekBeginDate  and DT <=  WeekEndDate  group by DT ";

You could try this one: 您可以尝试以下方法:

// Initially, we have to parse the weekBeginDate string to create a DateTime object
// and then we add six days to this DateTime object.
DateTime dt = DateTime.Parse(WeekBeginDate).AddDays(6);

// Then we assign the string representation of the DateTime object we have created before.
Chart1.Titles[0].Text = dt.ToString() ;

you can add Days this way, but your object should be DateTime not string : 您可以通过这种方式添加Days,但是您的对象应该是DateTime而不是string

    DateTime WeekBeginDate;

     if(this.Page.Request["WeekBeginDate"] != null)
     {                  
       WeekBeginDate = DateTime.Parse(this.Page.Request["WeekBeginDate"].ToString());

       Chart1.Titles[0].Text = WeekBeginDate.AddDays(6).ToString();

     }

您可以执行以下操作:

WeekBeginDate = DateTime.Parse(WeekBeginDate).AddDays(6).ToString()

Well, since WeekBeginDate is a string, you first need to look at converting it to a DateTime object. 好吧,由于WeekBeginDate是一个字符串,因此您首先需要查看将其转换为DateTime对象的情况。

you can use DateTime.Parse or DateTime.TryParse for that. 您可以为此使用DateTime.ParseDateTime.TryParse

Then you'd just use the AddDays method to do the actual work. 然后,您只需使用AddDays方法即可完成实际工作。

So I would have something like this: 所以我会有这样的事情:

protected void Page_Load(object sender, System.EventArgs e)
{
    string WeekBeginDate = "";

    if(this.Page.Request["WeekBeginDate"] != null)
    {                  
        WeekBeginDate = (string)this.Page.Request["WeekBeginDate"];
        Chart1.Titles[0].Text = WeekBeginDate ;

        DateTime actualBeginDate;
        if (DateTime.TryParse(WeekBeginDate, out actualBeginDate))
        {
            DateTime actualEndDate = actualBeginDate.AddDays(6);
            string WeekEndDate = actualEndDate.ToString(); // Pick your favorite string formatting
        }
    }
}

I chose TryParse instead of Parse since the WeekBeginDate is coming from the user and you can't really know for sure what format it will be in. So Parse may throw an exception. 我选择了TryParse而不是Parse因为WeekBeginDate来自用户,并且您不确定是否会采用哪种格式。因此Parse可能会引发异常。

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

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