These C# examples use Unix seconds for the portable baseline and show milliseconds only where the runtime exposes them naturally. The question to answer before copying a snippet is simple: what unit does the next API expect?

Get the current Unix time

long seconds = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long milliseconds = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

Convert a timestamp to UTC

var instant = DateTimeOffset.FromUnixTimeSeconds(1700000000);
string iso = instant.UtcDateTime.ToString("O");

Production note

DateTimeOffset carries an offset, which makes it safer than a timezone-naive DateTime at an API boundary.

Keep the unit in the field name when a value crosses a boundary: createdAtSeconds and createdAtMs are longer than createdAt, but much less mysterious during an incident.

Frequent questions:

Q: Should timestamp code use DateTime or DateTimeOffset?
A: Prefer DateTimeOffset for Unix timestamp work because it preserves offset context. It also exposes direct Unix seconds and milliseconds helpers.
Q: What does the O format do?
A: The O format writes a round-trip ISO 8601 value. It is useful for logs, JSON, and tests where the exact instant should survive parsing.
Q: How do I verify a C# conversion?
A: Convert the value back with DateTimeOffset.FromUnixTimeSeconds or FromUnixTimeMilliseconds, format it with O, and compare that UTC output with your expected timestamp before saving the value.