开发者

MessageBox.Show(TimeSpan)

开发者 https://www.devze.com 2022-12-26 07:44 出处:网络
i would like to show a TimeSpan in a MessageBox but am getting an error: DateTime date1 = new Da开发者_Go百科teTime(byear, bmonth, bday, 0, 0, 0);

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

DateTime date1 = new Da开发者_Go百科teTime(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);


you have to do age.ToString()


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号