时间跨度为datetime转换 [英] Timespan to dateTime conversion
问题描述
我想一个时间跨度转换为日期时间。 ?我怎样才能做到这一点。
I want to convert a Timespan to Datetime. How can I do this?
我找到了一个方法,在谷歌:
I found one method on Google:
DateTime dt;
TimeSpan ts="XXX";
//We can covnert 'ts' to 'dt' like this:
dt= Convert.ToDateTime(ts.ToString());
是否有任何其他方式做到这一点?
Is there any other way to do this?
推荐答案
这不是很符合逻辑的时间跨度转换为DateTime。试着去了解什么leppie上面说的。时间跨度是一个持续时间说6天5小时40分钟。这不是一个日期。如果我说6天;你可以推断出它的日期?答案是否定的,除非你有一个参考日期。
It is not very logical to convert TimeSpan to DateTime. Try to understand what leppie said above. TimeSpan is a duration say 6 Days 5 Hours 40 minutes. It is not a Date. If I say 6 Days; Can you deduce a Date from it? The answer is NO unless you have a REFERENCE Date.
所以,如果你想转换时间跨度为datetime你需要一个参考日期。 6天&安培;从5小时的时候?所以,你可以这样写:
So if you want to convert TimeSpan to DateTime you need a reference date. 6 Days & 5 Hours from when? So you can write something like this:
DateTime dt = new DateTime(2012, 01, 01);
TimeSpan ts = new TimeSpan(1, 0, 0, 0, 0);
dt = dt + ts;
这篇关于时间跨度为datetime转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!