C#中:什么是减去时间最简单的方法? [英] C#: what is the easiest way to subtract time?

查看:239
本文介绍了C#中:什么是减去时间最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把一个工具,这将有助于我做的工作日程。什么是解决以下最简单的方法?

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:00 + 5小时数=下午1:00

8:00am + 5 hours = 1:00pm

下午5:00 - 2小时数= 3:00

5:00pm - 2 hours = 3:00pm

下午5:30 - :45 = 4:45

5:30pm - :45 = 4:45

和等。

推荐答案

这些都可以用的 DateTime.Add(时间跨度) 因为它支持正面和负面的时间跨度。

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));

或者你也可以使用 DateTime.Subtract(时间跨度) 方法类似。

这篇关于C#中:什么是减去时间最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆