Coder Perfect

How to get the unix timestamp in C#


I have had look around stackoverflow, and even looked at some of the suggested questions and none seem to answer, how do you get a unix timestamp in C#?

Asked by bizzehdee

Solution #1

DateTimeOffset is available in.NET 4.6. ToUnixTimeSeconds.

Because this is an instance method, you must call it on a DateTimeOffset instance. You can also cast any DateTime object, but be aware of the timezone. To obtain the current timestamp, follow these steps:


To retrieve the timestamp from a DateTime, do the following:

DateTime foo = DateTime.Now;
long unixTime = ((DateTimeOffset)foo).ToUnixTimeSeconds();

Answered by Bob

Solution #2

In C#, you can retrieve a unix timestamp by subtracting the 1970-01-01 epoch time from DateTime.UtcNow.


Int32 unixTimestamp = (int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;

DateTime. Any DateTime object for which you want to acquire the unix timestamp can be substituted for UtcNow.

There’s also a field called DateTime.UnixEpoch, which isn’t well described by Microsoft but might be used in instead of new DateTime (1970, 1, 1)

Answered by bizzehdee

Solution #3

Ticks are another option. Because I’m writing for Windows Mobile, I don’t have access to all of the methods. I don’t have access to TotalSeconds.

long epochTicks = new DateTime(1970, 1, 1).Ticks;
long unixTime = ((DateTime.UtcNow.Ticks - epochTicks) / TimeSpan.TicksPerSecond);


TimeSpan epochTicks = new TimeSpan(new DateTime(1970, 1, 1).Ticks);
TimeSpan unixTicks = new TimeSpan(DateTime.UtcNow.Ticks) - epochTicks;
double unixTime = unixTicks.TotalSeconds;

Answered by Dave Hindle

Solution #4

This is how I do it:

public long UnixTimeNow()
    var timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
    return (long)timeSpan.TotalSeconds;

Remember that this method will return the time in Coordinated Universal Time (UTC) (UTC).

Answered by Bartłomiej Mucha

Solution #5

Truncating is the process of reducing anything to its simplest form. TotalSeconds is significant since it is specified as the current System’s value. In complete fractional seconds, the TimeSpan structure is given.

And how about a DateTime extension? Until property extensions are available, the second one is probably more complicated than it’s worth.

/// <summary>
/// Converts a given DateTime into a Unix timestamp
/// </summary>
/// <param name="value">Any DateTime</param>
/// <returns>The given DateTime in Unix timestamp format</returns>
public static int ToUnixTimestamp(this DateTime value)
    return (int)Math.Truncate((value.ToUniversalTime().Subtract(new DateTime(1970, 1, 1))).TotalSeconds);

/// <summary>
/// Gets a Unix timestamp representing the current moment
/// </summary>
/// <param name="ignored">Parameter ignored</param>
/// <returns>Now expressed as a Unix timestamp</returns>
public static int UnixTimestamp(this DateTime ignored)
    return (int)Math.Truncate((DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);

Answered by Brad

Post is based on