Our first program demonstrates how to get the number of seconds, milliseconds, or nanoseconds since the Unix epoch. Here’s how to do it in Visual Basic .NET.
In this example, we use DateTime.UtcNow to get the current UTC time. We then demonstrate how to convert this to seconds, milliseconds, and nanoseconds since the Unix epoch (January 1, 1970).
To run the program, save it as Epoch.vb and use the VB.NET compiler:
Note that Visual Basic .NET doesn’t have built-in methods for Unix time like UnixMilli or UnixNano. Instead, we calculate these values by subtracting the Unix epoch from the current time and converting the result to the appropriate unit.
Also, be aware that the nanosecond precision in this example is an approximation, as .NET’s DateTime type only has precision up to 100 nanoseconds (ticks).
Next, we’ll look at another time-related task: time parsing and formatting in Visual Basic .NET.