C# 如何从 DateTime 获取 TimeSpan

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

How to get TimeSpan from DateTime

c#.net

提问by Nil Pun

Scenario:

设想:

Third party web service returns datetimein two separate fields i.e. date and time. I need a way to concatenate into single field.

第三方 Web 服务datetime在两个单独的字段中返回,即日期和时间。我需要一种连接到单个字段的方法。

e.g.

 startDate='24-06-2012'
 startTime='1-01-1970 1:00:00 AM'

Expected result:
 fullStartDateTime='24-06-2012 1:00:00 AM'

I tried to get the TimeSpan part from startTime and got no where. Could someone let me know if there's a smart way to achieve above.

我试图从 startTime 获取 TimeSpan 部分,但无处可去。有人可以让我知道是否有一种聪明的方法可以实现上述目标。

采纳答案by McGarnagle

TimeOfDayis the property of DateTimethat you're looking for:

TimeOfDay是您正在寻找的DateTime的属性:

TimeSpan timeOfDay = startTime.TimeOfDay;
DateTime fullStartDateTime = startDate.Add(timeOfDay);

回答by Rami Mohammed

timespan fr = new timespan(); fr.parse(string date)

timespan fr = new timespan(); fr.parse(string date)