Converting Between Different Timestamp Types
There are different types of timestamps used in modern computer systems. One of, or potentially the most,
popular is the Unix timestamp, which counts either the seconds or milliseconds since an epoch, known as
the Unix epoch. The Unix epoch is on January 1, 1970 at 00:00:00 UTC (12:00:00 AM UTC).
Converting Between Epochs
If you have a Unix timestamp and want to convert it to a different epoch, you can do so by subtracting
the Unix timestamp of the new epoch from your timestamp you want to convert. For example... if you want
to convert the timestamp for the timestamp you requested above (1725892409) with the epoch of
Jauary 1, 2000 instead of January 1, 1970, you
can perform the steps below.
- Convert January 1, 2000 into a Unix timestamp (
946684800
)
- Get the Unix timestamp of the value you want to convert (in this case
1725892409
)
- Perform a simple subtraction (
1725892409 - 946684800 = 779207609
)
- You now have a timestamp with that epoch.
To convert it back to a timestamp that uses the Unix epoch, you can do the process in reverse.
- Convert January 1, 2000 into a Unix timestamp (
946684800
)
- Take your timestamp and add it (
779207609 + 946684800 = 1725892409
)
- You now have a timestamp with the Unix epoch.
Convert Unix and Mac HFS+ Timestamps
Mac HFS+ timestamps are simply "Unix timestamps" with a different epoch (i.e. the only difference between them is
that the epoch is January 1, 1904 (an epoch of -2082844800 in Unix time)). You
can follow the steps below to convert a Unix timestamp and Mac HFS+ timestamp to and fro.
- Convert January 1, 1904 into a Unix timestamp (
-2082844800
)
- Get the Unix timestamp of the value you want to convert (in this case
1725892409
)
- Perform a simple subtraction (
1725892409 - -2082844800 = 3808737209
)
- You now have a timestamp with that epoch.
To convert it back to a timestamp that uses the Unix epoch, you can do the process in reverse.
- Convert January 1, 1904 into a Unix timestamp (
-2082844800
)
- Take your timestamp and add it (
3808737209 + -2082844800 = 1725892409
)
- You now have a timestamp with the Unix epoch.
Convert Unix and Cocoa Core Timestamps
Like Mac HFS+ timestamps, the only difference is a different epoch (January 1, 2001).You
can follow the steps below to convert a Unix timestamp and Cocoa Core timestamp.
- Convert January 1, 2001 into a Unix timestamp (
978307200
)
- Get the Unix timestamp of the value you want to convert (in this case
1725892409
)
- Perform a simple subtraction (
1725892409 - 978307200 = 747585209
)
- You now have a timestamp with that epoch.
To convert it back to a timestamp that uses the Unix epoch, you can do the process in reverse.
- Convert January 1, 2001 into a Unix timestamp (
978307200
)
- Take your timestamp and add it (
747585209 + 978307200 = 1725892409
)
- You now have a timestamp with the Unix epoch.
See the Apple developer documentation for CFAbsoluteTime
for more information.
Convert a .NET Timestamp to a Unix Timestamp
.NET has the System.DateTimeOffset
class
which can be used to quickly convert between Unix timestamps to .NET timestamps and back again. The C# code example below
converts between Unix timestamps to a .NET timestamp.
using System;
const Int64 UnixTimestamp = 1725892409L;
DateTimeOffset datetime = DateTimeOffset.FromUnixTimeSeconds(UnixTimestamp);
Console.WriteLine(datetime.Ticks);
And it is just as simple to convert a .NET Timestamp to a Unix timestamp.
using System;
const Int64 DotNetTimestamp = 638614892094780000L;
DateTimeOffset datetime = new DateTimeOffset(DotNetTimestamp);
Console.WriteLine(datetime.ToUnixTimeSeconds());
If you have or want Unix time in milliseconds, you can use FromUnixTimeMilliseconds
and ToUnixTimeMilliseconds
instead.