C#:最简单的减法方法是什么? [英] C#: what is the easiest way to subtract time?
本文介绍了C#:最简单的减法方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
8:00 am + 5 hours = 1:00 pm
和
5:00 pm - 2小时= 3:00 pm
和
5:30 pm - :45 = 4:45
等等。
解决方案
这些都可以用 DateTime.Add(TimeSpan)
,因为它支持正负时间片。
DateTime original = new DateTime(年,月,日,8,0,0);
DateTime updated = original.Add(new TimeSpan(5,0,0));
DateTime original = new DateTime(年,月,日,17,0,0);
DateTime已更新= original.Add(new TimeSpan(-2,0,0));
DateTime original = new DateTime(年,月,日,17,30,0);
DateTime已更新= original.Add(new TimeSpan(0,45,0));
或者您也可以使用 DateTime.Subtract(TimeSpan)
类似的方法。 / p>
I'm trying to put together a tool that will help me make work schedules. What is the easiest way to solve the following?
8:00am + 5 hours = 1:00pm
and
5:00pm - 2 hours = 3:00pm
and
5:30pm - :45 = 4:45
and so on.
解决方案
These can all be done with DateTime.Add(TimeSpan)
since it supports positive and negative timespans.
DateTime original = new DateTime(year, month, day, 8, 0, 0);
DateTime updated = original.Add(new TimeSpan(5,0,0));
DateTime original = new DateTime(year, month, day, 17, 0, 0);
DateTime updated = original.Add(new TimeSpan(-2,0,0));
DateTime original = new DateTime(year, month, day, 17, 30, 0);
DateTime updated = original.Add(new TimeSpan(0,45,0));
Or you can also use the DateTime.Subtract(TimeSpan)
method analogously.
这篇关于C#:最简单的减法方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文