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

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

问题描述

我想在某个日期添加天数.我有这样的代码:

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();

但是这个代码不行,天数不加!我在做什么愚蠢的错误?

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

推荐答案

DateTime 是不可变的.这意味着你不能改变它的状态,而必须将一个操作的结果赋给一个变量.

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天全站免登陆