简体   繁体   中英

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. The variable always changes based on user's selection but i always would like to add 6 days to the current date variable. 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.

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 :

    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.

you can use DateTime.Parse or DateTime.TryParse for that.

Then you'd just use the AddDays method to do the actual work.

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.

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