In converting my VB6 code over to VB.NET, I was looking for an easy way to take the different between two DateTime items and display in hours and minutes. The TimeSpan gives a nice structure to dive into the difference and see exactly the interval, but I was hoping to find a way to output this in the same way that you can use the ToShortTimeString method of DateTime.
As it turns out, I found a posting by Jay Barlow that mentions converting the time different to a date time, and then using a custom format string to do the work so that I wouldn’t have to. Here is my new VB.NET code, where rec is a class that includes the start and end date and duration is a string that holds the information to be displayed:
If rec.endDate IsNot Nothing Then Dim ts As TimeSpan = CDate(rec.endDate).Subtract(rec.startDate) Dim dt As DateTime = DateTime.MinValue.Add(ts) duration = "Event duration: " + dt.ToString("H:mm") End If
Oh, and one more thing. Am I the only one in the world that thinks that iPad is the worst product name in history? Sure, it is so close to iPod so as to create brand recognition and draw comparisons, but I would just wonder what kind of names were shot down, and if any of them did not begin with the letter i.
By the way, Happy (Belated) Australia Day.
One thought on “Easy way to format the time difference between two dates”
best name evar!!!!!
cant wait till the iPad Max comes out!