[英]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.Parse或DateTime.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.