在 C# 中减去时间的最简单方法是什么?

发布于 2024-09-28 13:12:42 字数 168 浏览 0 评论 0原文

我正在尝试整合一个工具来帮助我制定工作时间表。解决以下问题的最简单方法是什么?

  • 上午 8:00 + 5 小时 = 下午 1:00
  • 下午 5:00 - 2 小时 = 下午 3:00
  • 下午 5:30 - :45 = 4:45

依此类推。

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
  • 5:00pm - 2 hours = 3:00pm
  • 5:30pm - :45 = 4:45

and so on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

你在我安 2024-10-05 13:12:42

这些都可以通过 DateTime.Add(TimeSpan) 完成 因为它支持正时间跨度和负时间跨度。

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) 方法类似。

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.

勿忘初心 2024-10-05 13:12:42

在此处查看所有 DateTime 方法: http://msdn.microsoft.com /en-us/library/system.datetime.aspx

Add 返回一个新的 DateTime,它将指定 TimeSpan 的值添加到此实例的值。

AddDays 返回一个新的 DateTime,它将指定的天数添加到此实例的值。

AddHours 返回一个新的 DateTime,它将指定的小时数添加到该实例的值上。

AddMilliseconds 返回一个新的 DateTime,它将指定的毫秒数添加到此实例的值。

AddMinutes 返回一个新的 DateTime,它将指定的分钟数添加到此实例的值。

AddMonths 返回一个新的 DateTime,它将指定的月数添加到此实例的值。

AddSeconds 返回一个新的 DateTime,它将指定的秒数添加到此实例的值。

AddTicks 返回一个新的 DateTime,它将指定的刻度数添加到此实例的值。

AddYears 返回一个新的 DateTime,它将指定的年数添加到此实例的值。

Check out all the DateTime methods here: http://msdn.microsoft.com/en-us/library/system.datetime.aspx

Add Returns a new DateTime that adds the value of the specified TimeSpan to the value of this instance.

AddDays Returns a new DateTime that adds the specified number of days to the value of this instance.

AddHours Returns a new DateTime that adds the specified number of hours to the value of this instance.

AddMilliseconds Returns a new DateTime that adds the specified number of milliseconds to the value of this instance.

AddMinutes Returns a new DateTime that adds the specified number of minutes to the value of this instance.

AddMonths Returns a new DateTime that adds the specified number of months to the value of this instance.

AddSeconds Returns a new DateTime that adds the specified number of seconds to the value of this instance.

AddTicks Returns a new DateTime that adds the specified number of ticks to the value of this instance.

AddYears Returns a new DateTime that adds the specified number of years to the value of this instance.

苯莒 2024-10-05 13:12:42

您好,如果您只想从 DateTime 中减去 Integer 值,那么您必须编写如下代码

DateTime.Now.AddHours(-2)

这里我从当前日期和时间中减去 2 小时

Hi if you are going to subtract only Integer value from DateTime then you have to write code like this

DateTime.Now.AddHours(-2)

Here I am subtracting 2 hours from the current date and time

千紇 2024-10-05 13:12:42

这也有效:

System.DateTime dTime = DateTime.Now();

// tSpan is 0 days, 1 hours, 30 minutes and 0 second.
System.TimeSpan tSpan = new System.TimeSpan(0, 1, 3, 0); 

System.DateTime result = dTime + tSpan;

减去一年:

DateTime DateEnd = DateTime.Now;
DateTime DateStart = DateEnd - new TimeSpan(365, 0, 0, 0);

This works too:

System.DateTime dTime = DateTime.Now();

// tSpan is 0 days, 1 hours, 30 minutes and 0 second.
System.TimeSpan tSpan = new System.TimeSpan(0, 1, 3, 0); 

System.DateTime result = dTime + tSpan;

To subtract a year:

DateTime DateEnd = DateTime.Now;
DateTime DateStart = DateEnd - new TimeSpan(365, 0, 0, 0);
骑趴 2024-10-05 13:12:42

使用 TimeSpan 对象捕获初始时间元素并使用 AddHoursAddMinutes 等方法。要减去 3 小时,您需要执行 AddHours(-3)。要减去 45 分钟,您需要执行 AddMinutes(-45)

Use the TimeSpan object to capture your initial time element and use the methods such as AddHours or AddMinutes. To substract 3 hours, you will do AddHours(-3). To substract 45 mins, you will do AddMinutes(-45)

笑脸一如从前 2024-10-05 13:12:42

试试这个

namespace dateandtime
{

    class DatesTime
    {

        public static DateTime Substract(DateTime now, int hours,int minutes,int seconds)
        {
            TimeSpan T1 = new TimeSpan(hours, minutes, seconds);
            return now.Subtract(T1);
        }


        static void Main(string[] args)
        {
            Console.WriteLine(Substract(DateTime.Now, 36, 0, 0).ToString());

        }
    }
}

try this

namespace dateandtime
{

    class DatesTime
    {

        public static DateTime Substract(DateTime now, int hours,int minutes,int seconds)
        {
            TimeSpan T1 = new TimeSpan(hours, minutes, seconds);
            return now.Subtract(T1);
        }


        static void Main(string[] args)
        {
            Console.WriteLine(Substract(DateTime.Now, 36, 0, 0).ToString());

        }
    }
}
情绪 2024-10-05 13:12:42

原始日期时间 = 新日期时间(年、月、日、8、0、0);
DateTime 更新 = Original.Add(new TimeSpan(5,0,0));

原始日期时间 = 新日期时间(年、月、日、17、0、0);
DateTime 更新 = Original.Add(new TimeSpan(-2,0,0));

原始日期时间 = 新日期时间(年、月、日、17、30、0);
DateTime 更新 = 原始.Add(new TimeSpan(0,-45,0));

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

百合的盛世恋 2024-10-05 13:12:42

TimeLeftToOpen= new TimeSpan(TimeLeftToOpen.Hours, TimeLeftToOpen.Minutes, TimeLeftToOpen.Seconds - 1);

TimeLeftToOpen= new TimeSpan(TimeLeftToOpen.Hours, TimeLeftToOpen.Minutes, TimeLeftToOpen.Seconds - 1);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文