如何减去日期时间? [英] How to subract the Hours in date time?
本文介绍了如何减去日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人,
我正在使用VS2010和c#语言我想减去使用以下代码的小时数,但这不起作用任何人帮助我如何这样做。
dateTime stTime;
stTime = DateTime.Now;
totalHours = 1.71;
stTime = stTime.AddHours(-totalHours);
谢谢提前!...
解决方案
对于使用DateTime类型的算术运算,您应该使用TimeSpan类型。
类似于:
DateTime myDateTime = DateTime.Now;
TimeSpan myTimeSpan = TimeSpan.FromHours( 1 。 71 );
DateTime myNewDateTime = myDateTime - myTimeSpan;
简单,自我声明。
< blockquote>可能还有其他一些问题。但是AddHour()与DateTime完美配合。
你可以使用DateTime Class的AddHour()函数。 To Substract只需将小时值与减号( - )一起传递为
DateTime dt = DateTime.Now;
DateTime dt1 = dt.AddHours(-1.71);
Dear All,
I am using VS2010 and c# Language i want to subtract the hours i using following code but that is not working any one help me how to do it.
dateTime stTime ;
stTime = DateTime.Now;
totalHours = 1.71;
stTime = stTime.AddHours(-totalHours);
Thanks Advance!...
解决方案
For arithmetic operations with DateTime type, you should use TimeSpan type.
Something like:
DateTime myDateTime = DateTime.Now; TimeSpan myTimeSpan = TimeSpan.FromHours(1.71); DateTime myNewDateTime = myDateTime - myTimeSpan;
Simple, and self-declarative.
There may be some other issue. But AddHour() works perfectly with DateTime.
You can Use AddHour() function of DateTime Class. To Substract Just pass the hour value with minus(-) sign As
DateTime dt = DateTime.Now; DateTime dt1 = dt.AddHours(-1.71);
这篇关于如何减去日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文