.NET - Convert the following date ranges in text form to a Double.

Posted on 2011-10-19
Last Modified: 2012-05-12
An application I'm working with produces date ranges in string format.  See the image below.  I need a little C# or VB.NET that converts the string values to double values.  For example:

10:00 AM - 12:30 PM becomes 2.5
9:00 AM - 10:00 AM becomes 1.0

Note that the string values are kind of nasty.  9:00 AM is represented as "9:00 AM" as opposed to "09:00 AM"

Question by:jdana
    LVL 85

    Accepted Solution

    If the second time is less than the first, then I assume the range spans Midnight:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            TextBox1.Text = "10:00 AM - 12:30 PM"
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim duration As Double = ComputeDuration(TextBox1.Text)
            Label1.Text = duration.ToString()
        End Sub
        Private Function ComputeDuration(ByVal timeRange As String) As Double
            Dim duration As Double = -1
            Dim dtStart, dtEnd As DateTime
            Dim values() As String = timeRange.Split("-")
            If values.Length = 2 Then
                If DateTime.TryParse(values(0).Trim, dtStart) AndAlso DateTime.TryParse(values(1).Trim, dtEnd) Then
                    If dtEnd < dtStart Then
                        dtEnd = dtEnd.AddDays(1)
                    End If
                    duration = dtEnd.Subtract(dtStart).TotalHours
                End If
            End If
            Return duration
        End Function
    End Class

    Open in new window

    LVL 29

    Assisted Solution

    May I make a very small suggestion? :)

    If option strict is on the line:

    Dim values() As String = timeRange.Split("-")

    gives an error. Change:

    Dim values() As String = timeRange.Split("-"c)
    LVL 29

    Expert Comment

    Also warning. If we have 23:45 PM - 2:30 PM ten the result is not correct.

    But may be this sequence is not valid...
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I get 14.75...which seems correct (*assuming we are going into the next day.)

    Author Closing Comment

    Awesome!  Thanks guys.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    731 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

    18 Experts available now in Live!

    Get 1:1 Help Now