1

I've coded a way to display a TimeSpan in VB.NET Framework 4.0 which look like this:

Me.lbl_StatsOverTimeSince.Text = TimeSpan.FromSeconds(My.Settings.stats_OverTimeSince).ToString("d\d\ h\h\ m\m\ s\s")

Now my problem is that I tried converting this application to 2.0 and it's the only thing that is not working.

I've seen this thread: Formatting TimeSpans

I tried tweaking with the suggested idea:

Dim ts As TimeSpan = TimeSpan.FromSeconds(My.Settings.stats_OverTimeSince)
Me.lbl_StatsOverTimeSince.Text = String.Format("{0:d\\d\\ h\\h\\ m\\m\ s\\s}", ts)

I figured the problem is that I'm working with TimeSpan.FromSeconds instead of New TimeSpan() because it displays 999.23:59:59 whatever the FromSeconds value is.

Is there any workaround?

Thanks in advance.

Community
  • 1
  • 1
Senerio
  • 35
  • 1
  • 9

1 Answers1

2

It's got nothing to do with using FromSeconds. A TimeSpan is a TimeSpan, regardless of how it was created.

The overloads of TimeSpan.ToString were only added in .NET 4.0. Your use of String.Format is still going to rely on TimeSpan.ToString internally. You'll have to do your own formatting:

Dim time = TimeSpan.FromSeconds(My.Settings.stats_OverTimeSince)

Me.lbl_StatsOverTimeSince.Text = String.Format("{0}d {1}h {2}m {3}s", time.Days, time.Hours, time.Minutes, time.Seconds)
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • Nice one! Thanks for your quick answer! I needed that to release my new version of the app. ^_^ – Senerio Aug 28 '14 at 03:24