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.

Dani AI

Generated

A few practical additions that make the start/end time -> minutes computation robust in an ASP.NET WebForms app:

  • Parse the text inputs on the server with an exact set of formats (accept 12-hour and 24-hour if needed). Do not rely only on client-side regex.
  • Remember that a DateTime parsed from a time-only string gets a date component (usually today). If the end time is earlier than the start time, treat that as crossing midnight (add one day) before subtracting.
  • Use the TimeSpan produced by subtraction and read TotalMinutes for a single minute value (use Math.Truncate/Math.Floor if you need an integer). Avoid reading ts.Minutes/ts.Hours when you want the whole duration in minutes.
  • Do not use MsgBox in server code; update a Label or return a validation error to the page. Also trim inputs and handle parse failures gracefully.

Example VB.NET pattern that covers parsing, overnight spans and a clean total-minutes result:

Public Sub SetDurationMinutes()
    Dim s As DateTime
    Dim e As DateTime
    Dim formats As String() = {"h:mm tt", "hh:mm tt", "H:mm", "HH:mm"}

    If Not DateTime.TryParseExact(starttimeInput.Text.Trim(), formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, s) Then
        durationLabel.Text = "Invalid start time"
        Return
    End If

    If Not DateTime.TryParseExact(endtimeInput.Text.Trim(), formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, e) Then
        durationLabel.Text = "Invalid end time"
        Return
    End If

    If e <= s Then
        e = e.AddDays(1)
    End If

    Dim ts As TimeSpan = e - s
    Dim totalMinutes As Integer = CInt(Math.Truncate(ts.TotalMinutes))
    durationLabel.Text = totalMinutes.ToString()
End Sub

This keeps parsing strict, avoids negative spans, and returns the single minutes value you originally requested. pointed to the right idea with Subtract/TotalMinutes; 's TimeSpan approach is fine but needs the empty-If and overnight checks fixed and server-side validation instead of MsgBox.

Recommended Answers

All 2 Replies

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.