Adding hours and minutes to timespan

Posted on 2006-05-03
Last Modified: 2008-03-03
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.
Question by:jamesr1411
    LVL 28

    Accepted Solution

    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)
    LVL 34

    Expert Comment

    by:Brian Crowe
    dim dec as decimal = 8.5

    myTimespan.add(new timespan(cint(dec.truncate), ((dec - dec.truncate) * 60).truncate, 0)
    LVL 62

    Assisted Solution

    by:Fernando Soto
    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

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now