How to convert .NET DateTime ticks to a Unix timestamp and back
By TechCompare · Updated · How we check
Subtract 621355968000000000, the tick count of January 1, 1970, then divide by 10,000,000 for Unix seconds or by 10,000 for milliseconds. That only works when the ticks came from a UTC DateTime.
A .NET tick is 100 nanoseconds, and DateTime.Ticks counts them from midnight on January 1 of the year 1. The example here, 639161082000000000, is June 3, 2026 at 18:30:00 UTC, which is Unix time 1780511400. Getting from one to the other takes a subtraction and a division.
How this is calculated
Microsoft's docs define the value as "the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 in the Gregorian calendar". Ticks skip leap seconds, the same as Unix time, so the gap between the two counts never changes:
(639161082000000000 - 621355968000000000) / 10000000 = 1780511400In C#, DateTime.UnixEpoch holds that starting point, from .NET Core 2.1 on, so you don't have to type the constant. DateTimeOffset converts in both directions:
long ticks = 639161082000000000;
long seconds = (ticks - DateTime.UnixEpoch.Ticks) / TimeSpan.TicksPerSecond; // 1780511400
var date = new DateTime(ticks, DateTimeKind.Utc);
long viaOffset = new DateTimeOffset(date).ToUnixTimeSeconds(); // 1780511400
long back = DateTimeOffset.FromUnixTimeSeconds(1780511400).UtcTicks; // 639161082000000000A tick count doesn't say which clock it came from. The same docs say a Local DateTime's ticks count "in the local time as specified by the current time zone setting", and a Utc one's count in UTC. The formula above assumes UTC.
Before 1970, the two C# routes can disagree. Integer division in C# rounds toward zero, so for a negative result the subtraction lands on the second after the moment. ToUnixTimeSeconds() returns the second that holds it. Half a second before the epoch, at 23:59:59.5 UTC on December 31, 1969, this code shows the gap:
long ticks = DateTime.UnixEpoch.Ticks - 5_000_000; // half a second before 1970
Console.WriteLine((ticks - DateTime.UnixEpoch.Ticks) / TimeSpan.TicksPerSecond); // 0
Console.WriteLine(new DateTimeOffset(ticks, TimeSpan.Zero).ToUnixTimeSeconds()); // -1When ticks leave .NET, in a database column or a JSON field, other languages can convert them too. Python's integers are exact, and its // rounds down, so the plain formula works for any date:
ticks = 639161082000000000
seconds = (ticks - 621355968000000000) // 10_000_000 # 1780511400JavaScript needs a BigInt, since a tick count is far past Number.MAX_SAFE_INTEGER. Read it from a string, because a plain number literal rounds the last digits before BigInt ever sees them. BigInt division rounds toward zero like C#, so the same care applies before 1970.
const ticks = BigInt("639161082000000000")
const ms = Number((ticks - 621355968000000000n) / 10_000n)
console.log(new Date(ms).toISOString()) // 2026-06-03T18:30:00.000ZTicks and a Windows FILETIME count the same 100-nanosecond steps, but FILETIME starts in 1601, at tick 504911232000000000. The FILETIME page covers that format and the Active Directory values that use it. For the negative timestamps themselves, see negative Unix timestamps.
The Unix Timestamp Converter shows the tick count for any moment in its Other Formats card. Paste ticks into that field to get the Unix timestamp and the date. If the ticks hold milliseconds, the timestamp switches to milliseconds. Anything finer is dropped, since the converter works in whole milliseconds.
Verdict
Go through DateTimeOffset when you can. UtcTicks and ToUnixTimeSeconds() always work in UTC, and the latter rounds down before 1970. When you do the math by hand, make sure the ticks came from a UTC value, floor the division for negative results, and keep the number in a 64-bit integer or a BigInt so its last digits survive.
More Timestamp scenarios
Related guides
Frequently asked questions
How many ticks are in a second?
What is the tick count of the Unix epoch?
DateTime.UnixEpoch.Ticks returns it, and DateTimeOffset.FromUnixTimeSeconds(0).UtcTicks gives the same number.Why is my converted time off by a few hours?
DateTime.Now. Its count includes the machine's offset from UTC, so the Unix time is off by that offset. Take ticks from DateTime.UtcNow, call ToUniversalTime() first, or use DateTimeOffset.UtcTicks.Are .NET ticks the same as a Windows FILETIME?
DateTime.FromFileTimeUtc() and ToFileTimeUtc(), which also check that the value fits.What's the largest date ticks can hold?
DateTimeOffset.FromUnixTimeSeconds() throws for any value above 253,402,300,799.Related tools
JSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use toolCron Generator
Visually build standard 5-part cron expressions or translate them into readable schedules.
Use toolText Encoding Converter
Convert text to Base64, binary, hex, URL encoding and HTML entities, and back again.
Use tool