Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从X天开始获取日期时间值?_C#_Date_Math_Datetime_Datetime Format - Fatal编程技术网

C# 从X天开始获取日期时间值?

C# 从X天开始获取日期时间值?,c#,date,math,datetime,datetime-format,C#,Date,Math,Datetime,Datetime Format,可能重复: 我想要 MyNewDateValue = MyDateNow - MyDateInteger; 范例 今天是2012年12月22日 如果MyDateIneger值为120,MyNewDateValue将返回120天前的日期时间 MyNewDateValue = MyDateNow.AddDays(-120); 或 请研究一下方法 或者总体上 DateTime nDaysAgo = DateTime.Today.AddDays(-N); // where N is the num

可能重复:

我想要

MyNewDateValue = MyDateNow - MyDateInteger;
范例 今天是2012年12月22日

如果MyDateIneger值为120,MyNewDateValue将返回120天前的日期时间

MyNewDateValue = MyDateNow.AddDays(-120);

请研究一下方法

或者总体上

DateTime nDaysAgo = DateTime.Today.AddDays(-N);
// where N is the number of days
试试这个

DateTime dt = new DateTime();
dt = DateTime.Now;
DateTime newdt = new DateTime();
TimeSpan tim = new TimeSpan(120,0,0,0,0);
newdt = dt.Add(tim);
MessageBox.Show(newdt.ToString());

ADD.timespan将帮助您从今天起增加或减少天数。

我相信上述问题的第三个答案将为您提供所需的信息:
DateTime oneTwentyDaysAgo = DateTime.Today.AddDays(-120);
DateTime nDaysAgo = DateTime.Today.AddDays(-N);
// where N is the number of days
DateTime dt = new DateTime();
dt = DateTime.Now;
DateTime newdt = new DateTime();
TimeSpan tim = new TimeSpan(120,0,0,0,0);
newdt = dt.Add(tim);
MessageBox.Show(newdt.ToString());