0

I have two text boxes starttimeInput and endtimeInput that have regularexpression validators to only accept input of time variables (11:00 am, 12:15 pm, etc) and then a durationLabel where I want to calculate between the start time and end time and present it to the user as minutes.

Here is my code:

Partial Class _Default
    Inherits System.Web.UI.Page
    Public Overloads Function DateDiff(ByVal Interval As [ DateInterval | String ],
        Dim duration, msg As DateTime
        Dim secondDate As Date
        Dim firstDate = starttimeInput.Text
        secondDate = endtimeInput.Text
        msg = durationLabel & DateDiff(DateInterval.Day, Now, secondDate)
        MsgBox(msg)
    End Function

Can anyone help me with this, because I've never written this type of function before and I'm kind of lost.

Thank you.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by dougancil
0

Use DateTime.Subtract() :

DateTime dt1 = DateTime.Now;
      DateTime dt2 = DateTime.UtcNow;
      int minutesBetween = (int)Math.Truncate(dt2.Subtract(dt1).TotalMinutes);
0

SK,

This is what I actually used and it works for me:

Public Sub StartTimeDif()
        Dim startTime As DateTime
        Dim endTime As DateTime
        If DateTime.TryParse(starttimeInput.Text, startTime) AndAlso DateTime.TryParse(endtimeInput.Text, endTime) Then
        End If
        Dim ts As TimeSpan = endTime - startTime
        mindurLabel.Text = If(ts.Minutes = 0, "", ts.Minutes.ToString)
        hourdurLabel.Text = If(ts.Hours = 0, "", ts.Hours.ToString)
    End Sub
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.