Our first example demonstrates extensive support for times and durations in C#. Here are some examples:
To run the program, save it as TimeExample.cs and use the C# compiler:
This will output various time-related information and calculations. The exact output will depend on the current time when you run the program, but it will demonstrate the various time and duration operations shown in the code.
In C#, we use the DateTime struct for representing a specific point in time, and TimeSpan for representing a duration. These types provide methods and properties for various time-related operations, similar to the Go time package.
The DateTimeKind enum in C# is somewhat analogous to the Location in Go, though it’s more limited (only specifying UTC, local, or unspecified).
C# doesn’t have nanosecond precision for times; the smallest unit is typically a tick (100 nanoseconds) or a millisecond, depending on the method used.
Next, we’ll look at the related idea of time relative to the Unix epoch.