.Maui Blog

VB-Net | Difference between two dateTime in hh:MM format

How to get difference between two date in hh:MM format with vb.net.

Dim dt As DateTime = DateTime.Now
Dim dt1 As DateTime = scadenza_pin
Dim duration As TimeSpan = dt1 - dt
Dim durationTicks As Long = Math.Abs(duration.Ticks / TimeSpan.TicksPerMillisecond)
Dim hours As Long = durationTicks / (1000 * 60 * 60)
Dim minutes As Long = (durationTicks - (hours * 60 * 60 * 1000)) / (1000 * 60)

If minutes < 0 Then
    hours -= 1
    minutes = 60 + minutes
End If

Console.WriteLine(hours.ToString("00") & ":" & minutes.ToString("00"))
Exit mobile version