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.

Use DateTime.Subtract() :

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.