• 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.
0
jamesr1411
Asked:
jamesr1411
2 Solutions
 
iboutchkineCommented:
1.Convert 8.5 hours string
 to HH:mm
it will be s="8:30"
 then
 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)
0
 
Brian CroweCommented:
dim dec as decimal = 8.5

myTimespan.add(new timespan(cint(dec.truncate), ((dec - dec.truncate) * 60).truncate, 0)
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)


Fernando
0

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