简体   繁体   中英

MessageBox.Show(TimeSpan)

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 ?

MessageBox.Show(age.ToString());

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

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);

你必须做age.ToString()

或者您可以执行Convert.ToString(age)来保持您现在的格式。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM