日期时间在C#中添加天 [英] Datetime in C# add days

查看:177
本文介绍了日期时间在C#中添加天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些日期添加天。我有一个code是这样的:

I want to add days in some date. I have a code like this:

DateTime endDate = Convert.ToDateTime(this.txtStartDate.Text); 
Int64 addedDays = Convert.ToInt64(txtDaysSupp.Text); 
endDate.AddDays(addedDays); 
DateTime end = endDate; 
this.txtEndDate.Text = end.ToShortDateString();

但是,这code不工作,天不会添加!我在做什么愚蠢的错误?

But this code is not working, days are not added! What the stupid mistake I'm doing?

推荐答案

日期时间是不变的。这意味着你不能改变它的状态,并有一个操作的结果分配给一个变​​量。

DateTime is immutable. That means you cannot change it's state and have to assign the result of an operation to a variable.

endDate = endDate.AddDays(addedDays);

这篇关于日期时间在C#中添加天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆