Skip to content
AgileLogics
6 min read

Dates, Times, and Regular Expressions

Prerequisites: String methods and formatting

Working with dates

VB's Date type is .NET's DateTime:

Module Program
    Sub Main()
        Dim start = New Date(2025, 3, 9, 14, 30, 0)
        Dim later = start.AddDays(10).AddHours(3)

        Console.WriteLine(start.ToString("yyyy-MM-dd HH:mm"))
        Console.WriteLine(later.ToString("yyyy-MM-dd HH:mm"))
        Console.WriteLine(start.DayOfWeek.ToString())
        Console.WriteLine(start.Month & " " & start.Year)

        Dim span = later - start
        Console.WriteLine($"{span.TotalHours} hours, {span.Days} days")
        Console.WriteLine(later > start)

        Dim deadline = #12/31/2025#
        Console.WriteLine((deadline - start.Date).Days & " days to go")
        Console.WriteLine(Date.DaysInMonth(2024, 2))
        Console.WriteLine(Date.IsLeapYear(2025))
    End Sub
End Module
2025-03-09 14:30
2025-03-19 17:30
Sunday
3 2025
243 hours, 10 days
True
297 days to go
29
False
  • Subtracting two dates gives a TimeSpan (TotalHours, Days, Minutes, ...).
  • Date.Now is the local time and Date.UtcNow is UTC. Store and compare times in UTC, and convert to local time only for display.
  • DateOnly and TimeOnly represent just a date or just a time.

Parsing dates

Imports System.Globalization

Module Program
    Sub Main()
        Dim d As Date
        If Date.TryParseExact("2025-03-09", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, d) Then
            Console.WriteLine(d.ToString("dddd d MMMM yyyy", CultureInfo.InvariantCulture))
        End If

        Console.WriteLine(Date.TryParseExact("09/03/2025", "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, d))
    End Sub
End Module
Sunday 9 March 2025
False

Parse with an exact format and a culture: "09/03/2025" is 9 March in the UK and 3 September in the US. Store dates in the ISO format yyyy-MM-dd whenever you can — the Unix Epoch Converter is handy for timestamps.

Regular expressions

Imports System.Text.RegularExpressions

Module Program
    Sub Main()
        Dim log = "2025-03-09 ERROR disk full; 2025-03-10 WARN cpu high; 2025-03-11 ERROR timeout"

        Console.WriteLine(Regex.IsMatch(log, "ERROR"))

        For Each m As Match In Regex.Matches(log, "(\d{4}-\d{2}-\d{2}) (\w+) ([^;]+)")
            Console.WriteLine($"{m.Groups(1).Value} | {m.Groups(2).Value,-5} | {m.Groups(3).Value}")
        Next

        Console.WriteLine(Regex.Replace("call 555-0100 or 555-0199", "\d{3}-\d{4}", "XXX-XXXX"))

        Dim email As New Regex("^(?<user>[\w.]+)@(?<domain>[\w.]+)$")
        Dim result = email.Match("ada@example.com")
        If result.Success Then Console.WriteLine($"user={result.Groups("user").Value} domain={result.Groups("domain").Value}")
    End Sub
End Module
True
2025-03-09 | ERROR | disk full
2025-03-10 | WARN  | cpu high
2025-03-11 | ERROR | timeout
call XXX-XXXX or XXX-XXXX
user=ada domain=example.com

VB strings have no backslash escapes, so regex patterns can be written exactly as they are — "\d{4}" means what it says. Test patterns with the Regex Tester. Create a Regex object once and reuse it when matching many strings.

Practice

Extract every hashtag (# followed by letters or digits) from "Loving #vbnet and #dotnet10 today #code" and print them without the #, separated by commas.

Show solution
Imports System.Text.RegularExpressions

Module Program
    Sub Main()
        Dim tags = From m As Match In Regex.Matches("Loving #vbnet and #dotnet10 today #code", "#(\w+)")
                   Select m.Groups(1).Value
        Console.WriteLine(String.Join(", ", tags))
    End Sub
End Module
vbnet, dotnet10, code