C# 添加或总和像 13:30+00:00:20=13:30:20 但如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/510778/
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-04 06:14:32  来源:igfitidea点击:

Add or Sum of hours like 13:30+00:00:20=13:30:20 but how?

c#.netalgorithmdatetime

提问by Penguen

I want to add seconds (00:00:02) or minutes (00:00:20) on datetime value (may be stored string type) but how? Examples:

我想在日期时间值(可能是存储的字符串类型)上添加秒(00:00:02)或分钟(00:00:20)但是如何?例子:

13:30+02:02:02= 15:32:02 ,
13:30+00:00:01= 13:30:01 ,
13:30+00:01:00=13:31:00 or 13:30 (not important) 

Can you help me? I need your cool algorithm :) Thanks again...

你能帮助我吗?我需要你的酷算法 :) 再次感谢...

采纳答案by alex2k8

If you choose to use the TimeSpan, be aware about the Days part:

如果您选择使用 TimeSpan,请注意 Days 部分:

TimeSpan t1 = TimeSpan.Parse("23:30");
TimeSpan t2 = TimeSpan.Parse("00:40:00");
TimeSpan t3 = t1.Add(t2);
Console.WriteLine(t3); // 1.00:10:00

With DateTime:

使用日期时间:

DateTime d1 = DateTime.Parse("23:30");
DateTime d2 = DateTime.Parse("00:40:00");
DateTime d3 = d1.Add(d2.TimeOfDay); 
Console.WriteLine(d3.TimeOfDay); // 00:10:00

回答by Robin Day

Not really sure what you're after, but can you not just use the built in functions to C#'s DateTime object?

不太确定你在追求什么,但你不能只使用 C# 的 DateTime 对象的内置函数吗?

DateTime myDate = DateTime.Now;

myDate = myDate.AddHours(1);
myDate = myDate.AddMinutes(30);
myDate = myDate.AddSeconds(45);

回答by cbp

myDateTimeVariable.Add(new TimeSpan(2,2,2));

回答by cruizer

use the TimeSpan structure. you can add TimeSpans together, or you can add a TimeSpan to a DateTime to produce a new DateTime.

使用 TimeSpan 结构。您可以将 TimeSpan 添加在一起,也可以将 TimeSpan 添加到 DateTime 以生成新的 DateTime。

回答by Thomas Eyde

Adding two datetimes from strings:

从字符串中添加两个日期时间:

var result = DateTime.Parse(firstDate) + DateTime.Parse(secondDate);

Adding a string time to a datetime:

将字符串时间添加到日期时间:

var result = existingDateTime.Add(TimeSpan.Parse(stringTime);

Adding time as in your example:

在您的示例中添加时间:

var result = TimeSpan.Parse("12:30:22") + TimeSpan.Parse("11:20:22");

Finally, your example as dates (not tested!):

最后,您的示例作为日期(未测试!):

var result = DateTime.Parse("12:30:22") + DateTime.Parse("11:20:22");

Note that this is sloppy coding, but you get the idea. You need to verify somehow that the string is actually parseable.

请注意,这是草率的编码,但您明白了。您需要以某种方式验证字符串实际上是可解析的。

回答by Sorskoot

You should have a look at TimeSpan.Parse. This converts a string to a TimeSpan object. That way you can do stuff like

你应该看看TimeSpan.Parse。这会将字符串转换为 TimeSpan 对象。这样你就可以做类似的事情

TimeSpan a = TimeSpan.Parse(timeStringA)+TimeSpan.Parse(TimeStringB);

To split a string like "00:00:20+00:01:00" look at string.split

要拆分像 "00:00:20+00:01:00" 这样的字符串,请查看string.split

stringA = timeSting.split('+')[0];
stringb = timeSting.split('+')[1];

回答by Vilx-

The problem is more abstract. As already mentioned, in .NET there are two types - DateTimeand TimeSpan. The DateTimetype represents a specific point in time. It's not an interval of time. It's a specific location in all time since the birth of the Universe. Even if you set the year/month/day components to 0, it will still represent some absolute point in time. Not a length of time.

这个问题比较抽象。如前所述,在 .NET 中有两种类型 -DateTimeTimeSpan. 该DateTime类型表示在特定时间点。这不是时间间隔。自宇宙诞生以来,它一直是一个特定的位置。即使您将年/月/日组件设置为 0,它仍将代表某个绝对时间点。不是时间长短。

The TimeSpanon the other hand represents some interval. 1 minute, 2 days, whatever. It's not specified WHEN, just HOW LONG.

TimeSpan另一方面表示一些间隔。1分钟,2天,随便。它没有指定何时,只是多久。

So if you were to subtract two DateTime objects you would get a TimeSpan object that specifies how much time there is between them. And if you add a TimeSpan to a DateTime you get another DateTime. But you can't add a DateTime to another DateTime - that would make no sense.

因此,如果您要减去两个 DateTime 对象,您将得到一个 TimeSpan 对象,该对象指定它们之间的时间。如果您将 TimeSpan 添加到 DateTime,您将获得另一个 DateTime。但是您不能将 DateTime 添加到另一个 DateTime - 这是没有意义的。

It sounds to me like you should be working with TimeSpans all the time, because you are dealing with lengths of time, not absolute points in time. If you get these lengths from your source as a DateTime then that's actually not correct, and you should convert them to TimeSpans somehow. The parsing method is one way that has been suggested, but you might also try to subtract zero DateTimefrom it. That might be faster and more culture-independant.

在我看来,您应该一直使用TimeSpans ,因为您正在处理时间长度,而不是绝对时间点。如果你从你的源中得到这些长度作为 DateTime 那么这实际上是不正确的,你应该以TimeSpan某种方式将它们转换为s 。解析方法是已建议的一种方法,但您也可以尝试从中减去零DateTime。这可能更快,更独立于文化。

回答by Sajjad

return string.Format("{0}:{1}:{2}", mytimespan.Hours 
    + (mytimespan.Days*24),mytimespan.Minutes,mytimespan.Seconds);

回答by Anuja Lamahewa

    static void Main(string[] args)
    {
        String timeText = "3/23/2015 12:00:13 AM"; 
        String timeText2 = "3/23/2015 1:45:03 AM"; 

        DateTime time = Convert.ToDateTime(timeText);
        string temp = time.ToString("HH:mm:ss");

        DateTime time2 = Convert.ToDateTime(timeText2);
        string temp2 = time2.ToString("HH:mm:ss");

        TimeSpan t1 = TimeSpan.Parse(temp);
        TimeSpan t2 = TimeSpan.Parse(temp2);

        Console.Out.WriteLine(t1 + t2);  // 01:45:16

        Console.ReadLine();
    }