如何从整数值中减去选定的日期 [英] how to subtract a selected date from an integer value
本文介绍了如何从整数值中减去选定的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试该代码.
i am trying that code.
dt.Subtract(new TimeSpan(totCount)).ToString();
但只减去一天,因为计数为1,2,3,4,5,6..etc
其结果类似于-
2011/12/16下午11:59:59但我想要像2011年12月14日星期三这样的结果
请帮帮我.
but it subtract only one day where as count is 1,2,3,4,5,6..etc
and its gives a result like-
12/16/2011 11:59:59 PM but i want result like Wednesday, December 14, 2011
Please help me out.
推荐答案
问题是您使用了错误的TimeSpan构造函数:Int64版本,它基于多个滴答声构造了TimeSpan. br/> 如果要减去天数,则不要构造时间跨度,而应使用带有负值参数的DateTime.AddDays方法:
The problem is that you are using the wrong TimeSpan constructor: the Int64 version, which constructs a TimeSpan based on a number of ticks.
If you want to subtract a number of days, then do not construct a timespan, use the DateTime.AddDays method with a negative value parameter instead:
int totalCount = 6;
DateTime dt = DateTime.Now;
dt = dt.AddDays(-totalCount);
您可以尝试
DateTime date = (DateTime.Now).AddDays(-2);
您可以用日期替换DateTime.Now
,用整数值替换2.
You can replace DateTime.Now
with your date and 2 with integer value.
^ ]可以为负数.
如果您的开始日期是2011年12月16日,那么
DateTime.AddDays[^] can take a negative number.
If your starting date is 12/16/2011 then
dt.AddDays(-2);
将是2001年12月14日
will be 12/14/2001
这篇关于如何从整数值中减去选定的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文