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,555
  • 4
  • 29
  • 54

2 Answers2

10

Use TimeSpan:

double seconds = 121.0005;
TimeSpan sp = TimeSpan.FromSeconds(seconds);
Console.Write(string.Format("{0} mins", sp.ToString(@"m\:ss")));
Tim Schmelter
  • 429,027
  • 67
  • 649
  • 891
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
  • 212,447
  • 27
  • 392
  • 421