Time Formatting Parsing in Visual Basic .NET

Imports System

Module TimeFormattingParsing
    Sub Main()
        ' Here's a basic example of formatting a time
        ' according to ISO 8601, which is similar to RFC3339.
        Dim currentTime As DateTime = DateTime.Now
        Console.WriteLine(currentTime.ToString("yyyy-MM-ddTHH:mm:ssK"))

        ' Time parsing uses a similar approach with format specifiers.
        Dim parsedTime As DateTime
        If DateTime.TryParseExact("2012-11-01T22:08:41+00:00", "yyyy-MM-ddTHH:mm:ssK", Nothing, Globalization.DateTimeStyles.None, parsedTime) Then
            Console.WriteLine(parsedTime)
        End If

        ' Format and Parse use format specifiers. You can use standard or custom format strings.
        Console.WriteLine(currentTime.ToString("h:mmtt"))
        Console.WriteLine(currentTime.ToString("ddd MMM dd HH:mm:ss yyyy"))
        Console.WriteLine(currentTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffK"))

        Dim customFormat As String = "h mm tt"
        If DateTime.TryParseExact("8 41 PM", customFormat, Nothing, Globalization.DateTimeStyles.None, parsedTime) Then
            Console.WriteLine(parsedTime)
        End If

        ' For purely numeric representations you can also
        ' use standard string formatting with the extracted
        ' components of the time value.
        Console.WriteLine("{0:yyyy-MM-ddTHH:mm:ss-00:00}", currentTime)

        ' TryParseExact will return False on malformed input.
        Dim success As Boolean = DateTime.TryParseExact("8:41PM", "ddd MMM d HH:mm:ss yyyy", Nothing, Globalization.DateTimeStyles.None, parsedTime)
        Console.WriteLine("Parsing succeeded: " & success.ToString())
    End Sub
End Module

Visual Basic .NET supports time formatting and parsing via format specifiers, which are similar to pattern-based layouts in other languages.

The DateTime structure in VB.NET provides methods for formatting and parsing time. The ToString method is used for formatting, while TryParseExact is used for parsing.

In the example above, we use DateTime.Now to get the current time and format it in various ways:

  1. ISO 8601 format (similar to RFC3339)
  2. Custom time format (3:04PM)
  3. Full date and time format
  4. High-precision format with timezone

For parsing, we use DateTime.TryParseExact. This method attempts to parse a string representation of a date and time using the specified format. If successful, it returns True and sets the output parameter to the parsed DateTime value.

The format specifiers in VB.NET are different from those used in the original example:

  • yyyy: 4-digit year
  • MM: 2-digit month
  • dd: 2-digit day
  • HH: 2-digit hour (24-hour format)
  • mm: 2-digit minute
  • ss: 2-digit second
  • fff: 3-digit millisecond
  • K: Time zone information

For purely numeric representations, you can use standard string formatting with the extracted components of the time value, as shown in the last Console.WriteLine statement.

If parsing fails due to malformed input, TryParseExact will return False, allowing you to handle the error gracefully.

This example demonstrates the flexibility of date and time handling in Visual Basic .NET, providing similar functionality to the original code while using language-specific constructs and methods.