• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3406
  • Last Modified:

Adding hours and minutes to timespan

How do you take hours and minutes that are in the form of 8.5( 8 hours 30 minutes ) for example and add it to an existing timespan.
2 Solutions
1.Convert 8.5 hours string
 to HH:mm
it will be s="8:30"
 Dim vals() As String = s.Split(":")
2.create a tiem span
 Dim ts As New TimeSpan(CInt(vals(0)), CInt(vals(1)), 0)
3.add this time span to existing tiime span (ExistingTS)
     ExistingTS= ExistingTS.Add(ts)
Brian CroweCommented:
dim dec as decimal = 8.5

myTimespan.add(new timespan(cint(dec.truncate), ((dec - dec.truncate) * 60).truncate, 0)
Fernando SotoCommented:
Hi jamesr1411;

This should do it.

        Dim DimExistingTS As New TimeSpan(1, 20, 0)
        Dim TimeToAdd As String = "8.5"
        Dim Hours As Integer = CType( _
            TimeToAdd.Substring(0, TimeToAdd.IndexOf(".")), Integer)
        Dim Minutes As Integer = CType(CType( _
            TimeToAdd.Substring(TimeToAdd.IndexOf(".")), Double) * 60, Integer)
        Dim ts As New TimeSpan(Hours, Minutes, 0)

        DimExistingTS = DimExistingTS.Add(ts)


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now