?
Solved

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

Posted on 2011-10-19
5
Medium Priority
?
204 Views
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"

Thanks.
TimeRange.png
0
Comment
Question by:jdana
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 900 total points
ID: 36997001
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

0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 100 total points
ID: 36997690
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)
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36997697
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...
0
 
LVL 86

Expert Comment

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

Author Closing Comment

by:jdana
ID: 37056032
Awesome!  Thanks guys.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 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