结合日期和时间日期是DateTime和时间是一个字符串 [英] Combine date and time when date is a DateTime and time is a string
问题描述
我与其中一个日期存储(没有时间),为日期时间和时间存储为串(无日期)的旧MySQL数据库工作
I am working with an old mysql database in which a date is stored (without a time) as a datetime and a time is stored as a string (without a date).
在C#我再有像 2010-06-25 12:00:00 AM
的值,像<$ C $值的字符串一个DateTime C> 15:02 。
In C# I then have a DateTime with a value like 2010-06-25 12:00:00 AM
and a String with a value like 15:02
.
什么是这些没有大量的开销相结合的最简洁的方式?
What is the most concise way to combine these without a lot of overhead?
我已经尝试了一些方法,包括:
I have tried a few methods including:
DateTime NewDateTime = DateTime.Parse(OldDateTime.ToString("yyyy-MM-dd ") + TimeString);
我不喜欢现有的日期时间转换为字符串和追加的时间。
I dislike converting the existing DateTime to a string and appending the time.
我的时间字符串转换为日期,但后来我得到今天的日期,并把它加为一些蜱的旧日期时间不正确。
I can convert the time string to a date, but then I get today's date and adding it as a number of ticks to the old datetime is incorrect.
的注意:不要担心验证,这是在其他地方完成。时间是使用24小时制,没有秒表示。的
推荐答案
您可以使用的 TimeSpan.Parse 来解析时间,然后将结果加入到日期
You can use TimeSpan.Parse to parse the time, and then add the result to the date:
DateTime newDateTime = oldDateTime.Add(TimeSpan.Parse(timeString));
这篇关于结合日期和时间日期是DateTime和时间是一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!