How to generate a UTC Unix Timestamp in C#

Paedow picture Paedow · Oct 10, 2012 · Viewed 27.4k times · Source

Possible Duplicate:
How to convert UNIX timestamp to DateTime and vice versa?

How can I create a unix timestamp in C#? (e.g. 2012-10-10 14:00:00 -> 1349877600)

Answer

John Woo picture John Woo · Oct 10, 2012
private double ConvertToTimestamp(DateTime value)
{
    //create Timespan by subtracting the value provided from
    //the Unix Epoch
    TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

    //return the total seconds (which is a UNIX timestamp)
    return (double)span.TotalSeconds;
}