converting string to timespan

Posted on 2006-06-05
Last Modified: 2010-05-19
Hello experts I have a value stored in a database as a string like so: hh:mm:ss  for example 00:34:32 which is 34 minutes and 32 seconds.  I need to convert this into a hours like so .5 hours.  How can I accomplish this?  
Question by:tentavarious
    LVL 9

    Expert Comment

    Try this...
    Dim myTime As DateTime = "#00:34:32#"
    Dim myAltTime As String = CStr(Math.Round(CInt(myTime.Minute) / 60, 1)) & " hour(s)")

    It should output "0.5 Hour(s)"
    LVL 16

    Accepted Solution

    Here is a more generic solution

    string s = "00:34:32";  //your input is a string, right?
    string[] strArray = s.Split(new char[] {':'});  //separate the string into three parts
    TimeSpan ts = new TimeSpan(
          Convert.ToInt32(strArray[2]));  //create the time span
    double nHour = ts.TotalHours;  //number of hours in double
    nHour = Math.Floor(nHour/0.5) * 0.5;  //do this if you want to round down to half hour

    Hope that helps

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    758 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

    10 Experts available now in Live!

    Get 1:1 Help Now