Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a double into a dateTime value in c#? [duplicate]

Tags:

c#

datetime

I would like to convert a double in to a datetime.

This is not a conversion from Excel. I have a double representing seconds and would simply like it represented as time in minutes and seconds.

121.0005 = 2:01 mins

like image 401
Julian Dormon Avatar asked Dec 02 '22 20:12

Julian Dormon


2 Answers

Use TimeSpan:

double seconds = 121.0005;
TimeSpan sp = TimeSpan.FromSeconds(seconds);
Console.Write(string.Format("{0} mins", sp.ToString(@"m\:ss")));
like image 197
Tim Schmelter Avatar answered May 01 '23 20:05

Tim Schmelter


Instead of DateTime what you need is TimeSpan, since your input is representing a time value, not Date.

Use TimeSpan.FromSeconds method.

double sec = 121.0005;
TimeSpan ts = TimeSpan.FromSeconds(sec);
like image 26
Habib Avatar answered May 01 '23 20:05

Habib