4

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

Julian Dormon
  • 1,767
  • 4
  • 32
  • 58

2 Answers2

11

Use TimeSpan:

double seconds = 121.0005;
TimeSpan sp = TimeSpan.FromSeconds(seconds);
Console.Write(string.Format("{0} mins", sp.ToString(@"m\:ss")));
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
7

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);
Habib
  • 219,104
  • 29
  • 407
  • 436