Senerio
Senerio

Reputation: 45

VB.Net Formatting TimeSpan with .Net Framework 2.0

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.

Upvotes: 1

Views: 822

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54417

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)

Upvotes: 2

Related Questions