如何在 C# 中添加时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1026654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:51:00  来源:igfitidea点击:

How do I add times in C#?

c#datetimedate-arithmetic

提问by

How do I add times in C#? For example:

如何在 C# 中添加时间?例如:

Time = "14:20 pm" +  "00:30 pm"

回答by James

You would want to convert both times into a TimeSpan objects.

您可能希望将这两个时间都转换为 TimeSpan 对象。

This will give you explicit access to the Hours/Minutes values of each time and you can add them together.

这将使您能够显式访问每次的小时/分钟值,您可以将它们相加。

See TimeSpanfrom MSDN.

请参阅MSDN 中的TimeSpan

回答by Colin Pickard

Assuming you want to add 30 minutes to a given DateTime, you can use AddMinutes.

假设您想为给定的 DateTime 添加 30 分钟,您可以使用AddMinutes

TestTime.AddMinutes(30);

Another way of doing it:

另一种方法:

DateTime TestTime = DateTime.Parse("22 Jun 2009 14:20:00");
// Add 30 minutes
TestTime = TestTime + TimeSpan.Parse("00:30:00");

回答by Kirschstein

 TimeSpan t1 = new TimeSpan(14, 20,0);
 TimeSpan t2 = new TimeSpan(0,30,0);
 Console.Out.WriteLine(t1 + t2);

回答by MSalters

You can't add those, just like you can't add "14:20PM" and the color red. You can add a time and a timespan (14:20PM + 30 minutes) or two timespans (2 hours+30 minutes). But you cannot add two times.

你不能添加这些,就像你不能添加“14:20PM”和红色一样。您可以添加一个时间和一个时间跨度(下午 14:20 + 30 分钟)或两个时间跨度(2 小时 + 30 分钟)。但是不能加两次。

To make this even clearer, consider what would happen if you could add two times: 14.20 + 00:30 (EST) = 23.20 + 09:30 (UTC)

为了更清楚地说明这一点,请考虑如果可以添加两次会发生什么情况:14.20 + 00:30 (EST) = 23.20 + 09:30 (UTC)

回答by weiqure

Try this (although 0:30pm doesn't make sense):

试试这个(虽然 0:30pm 没有意义):

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new StringTime("14:20 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("15:00 pm").Add(new StringTime("0:30 pm")));
        Console.WriteLine(new StringTime("5:00am").Add(new StringTime("12:00pm")));
    }
}

class StringTime
{
    public int Hours { get; set; }
    public int Minutes { get; set; }
    public bool IsAfternoon { get; set; }

    public StringTime(string timeString)
    {
        IsAfternoon = timeString.Contains("pm");
        timeString = timeString.Replace("pm", "").Replace("am", "").Trim();

        Hours = int.Parse(timeString.Split(':')[0]);
        Minutes = int.Parse(timeString.Split(':')[1]);
    }

    public TimeSpan ToTimeSpan()
    {
        if (IsAfternoon)
        {
            if (Hours < 12)
            {
                Hours += 12;
            }
        }
        return new TimeSpan(Hours, Minutes, 00);
    }

    public TimeSpan Add(StringTime time2)
    {
        return this.ToTimeSpan().Add(time2.ToTimeSpan());
    }
}

Output (the value before the dot are days):

输出(点前的值为天):

1.02:50:00
1.03:30:00
17:00:00

1.02:50:00
1.03:30:00
17:00:00