Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a UTC Unix Timestamp in C# [duplicate]

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)

like image 789
Paedow Avatar asked Oct 10 '12 14:10

Paedow


People also ask

How do I get Unix Timest C?

time() function in C The time() function is defined in time. h (ctime in C++) header file. This function returns the time since 00:00:00 UTC, January 1, 1970 (Unix timestamp) in seconds. If second is not a null pointer, the returned value is also stored in the object pointed to by second.

What is UTC Unix timestamp?

Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC. One of the primary benefits of using Unix time is that it can be represented as an integer making it easier to parse and use across different systems.

Is UTC the same as UNIX time?

Unix timestamps are always based on UTC (otherwise known as GMT). It is illogical to think of a Unix timestamp as being in any particular time zone. Unix timestamps do not account for leap seconds.

What timestamp is UTC?

UNIX timestamp (A.K.A. Unix's epoch) means elapsed seconds since January 1st 1970 00:00:00 UTC (Universal Time). So , if you need the time in a specific TimeZone, you should convert it.


1 Answers

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;
}
like image 169
John Woo Avatar answered Sep 24 '22 07:09

John Woo