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

查看:27
本文介绍了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(TimeSpan) 因为它支持正负时间跨度.

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(TimeSpan) 方法类似.

Or you can also use the DateTime.Subtract(TimeSpan) method analogously.

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

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