Convert TimeSpan to DateTime

I want to convert 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?

+42
c # datetime timespan
Apr 23 '12 at 7:10
source share
9 answers

Incorrectly converting TimeSpan to DateTime. Try to understand what leppie said above. TimeSpan duration say 6 days 5 hours 40 minutes. This is not a date. If I say 6 days; Can you infer a date from it? The answer is NO unless you have a REFERENCE DATE.

So, if you want to convert TimeSpan to DateTime, you need a key date. 6 days and 5 hours from then? 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; 
+70
Apr 23 '12 at 7:17
source share
— -

Despite the fact that the selected answer is strictly correct, I believe that I understand that the OP is trying to get here, because I had a similar problem.

I had a TimeSpan that I wanted to display in a grid control (as soon as hh: mm), but the grid did not seem to understand TimeSpan, but only DateTime. The OP has a similar scenario where only TimeSpan is an important part, but does not take into account the need to add a DateTime breakpoint.

So, as stated above, I just added DateTime.MinValue (although any date will do), which is then ignored by the grid when it displays the time as a temporary part of the resulting date.

+25
Mar 06 '13 at 2:05
source share

For this you need a reference date.

Example from http://msdn.microsoft.com/en-us/library/system.datetime.add.aspx

 // Calculate what day of the week is 36 days from this instant. System.DateTime today = System.DateTime.Now; System.TimeSpan duration = new System.TimeSpan(36, 0, 0, 0); System.DateTime answer = today.Add(duration); System.Console.WriteLine("{0:dddd}", answer); 
+7
Apr 23 '12 at 7:26
source share

To achieve this, TimeSpan can be added to the new DateTime.

 TimeSpan ts="XXX"; DateTime dt = new DateTime() + ts; 

But, as mentioned earlier, this is not strictly logical without a valid start date. I came across in the case when I need only the time aspect. will work fine if the logic is correct.

+3
Oct 23 '14 at 11:58
source share

If you need to show only the time value in the datagrid or label method, the best way is to convert the time directly to datetime datatype.

SELECT CONVERT (datetime, myTimeField) as myTimeField FROM Table1

0
Apr 24 '17 at 14:48
source share
 var StartTime = new DateTime(item.StartTime.Ticks); 
0
Nov 13 '17 at 5:27
source share

The problem with all of the above is that the conversion returns the wrong number of days specified in TimeSpan.
Using the above, the following returns 3, not 2.

Ideas on how to save 2 days in TimeSpan arguments and return them as DateTime day?

 public void should_return_totaldays() { _ts = new TimeSpan(2, 1, 30, 10); var format = "dd"; var returnedVal = _ts.ToString(format); Assert.That(returnedVal, Is.EqualTo("2")); //returns 3 not 2 } 
-3
Jun 09 '13 at 18:01
source share

Easy method, use tics:

 new DateTime((DateTime.Now - DateTime.Now.AddHours(-1.55)).Ticks).ToString("HH:mm:ss:fff") 

This function will give you a date (no day / month / year)

-3
04 Feb '15 at 21:16
source share

First convert the time interval to a string, then to a DateTime, then go back to the string:

 Convert.ToDateTime(timespan.SelectedTime.ToString()).ToShortTimeString(); 
-four
Jul 17 '14 at 23:06
source share



All Articles