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.
精彩评论