C# 从 X 天获取日期时间值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14008778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get datetime value from X days go?
提问by 001
Possible Duplicate:
c#: whats the easiest way to subtract time?
可能的重复:
c#:减时间最简单的方法是什么?
I want
我想要
MyNewDateValue = MyDateNow - MyDateInteger;
Example Today is the 22nd of December 2012
示例 今天是 2012 年 12 月 22 日
If MyDateIneger value is 120, MyNewDateValue, will return the datetime 120 days ago.
如果 MyDateIneger 值为 120,则 MyNewDateValue 将返回 120 天前的日期时间。
采纳答案by Jason Whitted
MyNewDateValue = MyDateNow.AddDays(-MyDateInteger);
回答by Seany84
MyNewDateValue = MyDateNow.AddDays(-120);
or
或者
MyNewDateValue = MyDateNow.AddDays(myVar);
回答by pala?н
Please look into DateTime.AddDays
method
请查看DateTime.AddDays
方法
DateTime oneTwentyDaysAgo = DateTime.Today.AddDays(-120);
or in general
或一般
DateTime nDaysAgo = DateTime.Today.AddDays(-N);
// where N is the number of days
回答by Uthistran Selvaraj
Try this frnd
试试这个
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 will help you to add or subtract days from today.
ADD.timespan 将帮助您从今天开始增加或减少天数。