Home → Unix Timestamp in C# / .NET
Unix Timestamp in C# / .NET
Modern .NET provides direct Unix timestamp methods on DateTimeOffset. This avoids a lot of manual conversion code and makes the intended timezone semantics explicit.
Working example
DateTimeOffset is generally preferable when you need an instant plus a clear offset context. Its Unix conversion methods work directly with epoch seconds and milliseconds.
Unit and timezone checks
In .NET, the important context is the epoch, the numeric unit, and whether the value is being converted through DateTimeOffset or a local DateTime. Keeping those concepts separate avoids offset surprises.
- Confirm whether the API expects seconds or milliseconds.
- When converting a calendar value in .NET, prefer an offset-aware value such as DateTimeOffset so the instant is not detached from its offset.
- Keep UTC as the reference when comparing distributed logs.
- Do not infer the unit solely from digit count.
Common mistakes in C# / .NET
- Passing milliseconds where an API expects seconds.
- Formatting a local time as if it were UTC.
- Dropping fractional seconds when the application requires millisecond precision.
- Using manual UTC offsets instead of the runtime's timezone-aware facilities.
When to use Unix timestamps
Unix values fit well at .NET API boundaries, telemetry records, and database serialization; keep DateTimeOffset for application logic where the exact instant matters.
Related Timestamp Tools
Unix Timestamps in C# and .NET: Practical Patterns
Modern .NET provides direct Unix-time methods on DateTimeOffset, which makes the unit explicit and avoids a lot of manual offset arithmetic. This is usually preferable to subtracting a hand-written epoch value from a local DateTime, because the offset-aware type communicates that the value represents an exact instant.
Useful APIs
For seconds, DateTimeOffset.UtcNow.ToUnixTimeSeconds() returns Unix seconds. For milliseconds, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() is appropriate when the receiving system expects millisecond precision. To convert back, DateTimeOffset.FromUnixTimeSeconds(value) or the millisecond equivalent creates an offset-aware value that can then be formatted for display.
Example code
Quick reference
Practical checks
- Prefer DateTimeOffset when working with an exact instant.
- Match the method to the source unit.
- Format using an explicit timezone when output is for people.
- Test serialization/deserialization with known UTC values.
Frequently Asked Questions: Practical Details
Should I use DateTime or DateTimeOffset?
For an exact instant and Unix conversion, DateTimeOffset is generally clearer because it carries offset context.
How do I convert Unix milliseconds in .NET?
Use the ToUnixTimeMilliseconds and FromUnixTimeMilliseconds methods.
Why avoid manual epoch subtraction?
Manual arithmetic can accidentally mix local time, UTC, and offset handling. The built-in methods encode the intended Unix-time operation more directly.
Frequently Asked Questions
How does C# / .NET represent the current Unix time?
Use the DateTimeOffset facilities shown on this page and confirm the returned unit in the language documentation.
Which .NET Unix conversion method matches an API contract?
Use the .NET method that matches the documented API unit, such as UnixTimeSeconds or UnixTimeMilliseconds, rather than inferring the unit from the number itself.
How should distributed .NET systems handle timezone data?
For distributed .NET systems, compare DateTimeOffset values on a common timeline and localize only for presentation.