-1

i would like to show a TimeSpan in a MessageBox but am getting an error:

DateTime date1 = new DateTime(byear, bmonth, bday, 0, 0, 0);
DateTime datenow =  DateTime.Now;
TimeSpan age = datenow - date1;
MessageBox.Show(ToString(age));

Error 1 No overload for method 'ToString' takes '1' arguments

how do i output a messagebox with TimeSpan?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

4 Answers4

11
MessageBox.Show(age.ToString());

Though you might not like the result. If you want a specific format you have to implement it yourself.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
3

That's not going to look great, TimeSpan is missing a decent ToString() override on .NET 3.5 and earlier. Work around that by using the DateTime.ToString() method:

  string txt = new DateTime(Math.Abs(age.Ticks)).ToString("h:mm:ss");
  if (age.Ticks < 0) txt = "-" + txt;
  MessageBox.Show(txt);
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

you have to do age.ToString()

RvdK
  • 19,580
  • 4
  • 64
  • 107
1

or you can do Convert.ToString(age) to keep with the format that you have now.

Jim
  • 3,425
  • 9
  • 32
  • 49