Imports System
Public Class Program
Public Shared Function ToRelativePastTime(userDate As DateTime) As String
Dim now = DateTime.UtcNow
Dim ts = now - userDate
Dim isFuture As Boolean = ts.TotalSeconds < 0
ts = ts.Duration() ' absolute value
Const SECOND As Integer = 1
Const MINUTE As Integer = 60 * SECOND
Const HOUR As Integer = 60 * MINUTE
Const DAY As Integer = 24 * HOUR
Const MONTH As Integer = 30 * DAY
Dim delta As Double = ts.TotalSeconds
Dim suffix As String = If(isFuture, "from now", "ago")
If delta < 1 * MINUTE Then
Return If(ts.Seconds <= 1,
$"one second {suffix}",
$"{ts.Seconds} seconds {suffix}")
End If
If delta < 2 * MINUTE Then
Return $"one minute {suffix}"
End If
If delta < 45 * MINUTE Then
Return $"{ts.Minutes} minutes {suffix}"
End If
If delta < 90 * MINUTE Then
Return $"one hour {suffix}"
End If
If delta < 24 * HOUR Then
Return $"{ts.Hours} hours {suffix}"
End If
If delta < 48 * HOUR Then
Return If(isFuture, "tomorrow", "yesterday")
End If
If delta < 30 * DAY Then
Return $"{ts.Days} days {suffix}"
End If
If delta < 12 * MONTH Then
Dim months As Integer = CInt(Math.Floor(ts.Days / 30.0))
Return If(months <= 1,
$"one month {suffix}",
$"{months} months {suffix}")
End If
Dim years As Integer = CInt(Math.Floor(ts.Days / 365.0))
Return If(years <= 1,
$"one year {suffix}",
$"{years} years {suffix}")
End Function
Public Shared Sub Main(args As String())
Console.WriteLine(DateTime.UtcNow)
Console.WriteLine(ToRelativePastTime(New DateTime(2026, 4, 13, 7, 29, 9)))
Console.WriteLine(ToRelativePastTime(New DateTime(2026, 4, 13, 7, 27, 11)))
Console.WriteLine(ToRelativePastTime(New DateTime(2026, 4, 13, 19, 5, 4)))
Console.WriteLine(ToRelativePastTime(New DateTime(2026, 4, 13, 11, 48, 0)))
Console.WriteLine(ToRelativePastTime(New DateTime(2026, 4, 9, 12, 0, 0)))
Console.WriteLine(ToRelativePastTime(New DateTime(2025, 4, 26, 12, 0, 0)))
Console.WriteLine(ToRelativePastTime(New DateTime(2013, 4, 26, 12, 0, 0)))
End Sub
End Class
' run:
'
' 04/13/2026 07:29:33
' 24 seconds ago
' 2 minutes ago
' 11 hours from now
' 4 hours from now
' 3 days ago
' 11 months ago
' 12 years ago
'