Click here to Skip to main content
15,867,973 members
Articles / Programming Languages / C#
Article

UNIX timestamp to System.DateTime

Rate me:
Please Sign up or sign in to vote.
3.15/5 (18 votes)
10 Apr 2005 222.3K   15   14
Converting UNIX timestamp to System.DateTime.

Introduction

This article is a very simple example of how to convert a UNIX timestamp to a System.DateTime in Visual C#.

The UNIX timestamp

The UNIX timestamp represents the time measured in number of seconds since the Unix Epoch (1st of January 1970 00:00:00 GMT), and is well-known to PHP-developers.

How to convert UNIX timestamp to System.DateTime

Given below is an example of how to convert any given UNIX timestamp to a System.DateTime.

C#
// This is an example of a UNIX timestamp for the date/time 11-04-2005 09:25.
double timestamp = 1113211532;

// First make a System.DateTime equivalent to the UNIX Epoch.
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

// Add the number of seconds in UNIX timestamp to be converted.
dateTime = dateTime.AddSeconds(timestamp);

// The dateTime now contains the right date/time so to format the string,
// use the standard formatting methods of the DateTime object.
string printDate = dateTime.ToShortDateString() +" "+ dateTime.ToShortTimeString();

// Print the date and time
System.Console.WriteLine(printDate);

How to convert System.DateTime to UNIX timestamp

I know that this part is just as essential as the above, but it seems that I am running out of time... I will of course return with an update on how to convert the System.DateTime to a UNIX timestamp.

Any comments or suggestions are welcomed!

This is my first article on Visual C# ever, so bare with me...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Other
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalummm actually... Pin
Jonathan Lathigee22-Jan-10 10:23
Jonathan Lathigee22-Jan-10 10:23 
... the best solution is:

public static DateTime UNIXTimeToDateTime(double unixTime)
{
    return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(unixTime).ToLocalTime();
}


(.AddMilliseconds, not .AddSeconds)

with comments, this article becomes very useful!!
GeneralJavascript-time and UNIX-Epoch-time start at the same Epoch but are measured with a different scale Pin
protron1-Mar-11 9:54
protron1-Mar-11 9:54 
GeneralMy vote of 1 Pin
Deceptor114-Jan-10 10:04
Deceptor114-Jan-10 10:04 
Generalactually, none of you bitches are correct Pin
Emil Müller4-Apr-08 11:25
Emil Müller4-Apr-08 11:25 
GeneralRe: actually, none of you bitches are correct Pin
Mikhail-T8-May-12 7:13
Mikhail-T8-May-12 7:13 
QuestionIs this correct? Pin
toebens5-Oct-06 2:39
toebens5-Oct-06 2:39 
GeneralVery Useful article Pin
MudkiSekhon28-Dec-05 23:13
MudkiSekhon28-Dec-05 23:13 
GeneralCode sample not correct Pin
ricky82 14-Jul-05 0:12
ricky82 14-Jul-05 0:12 
GeneralDateTime to Unix Timestamp Pin
EiswuerfelJr29-Apr-05 7:02
EiswuerfelJr29-Apr-05 7:02 
GeneralRe: DateTime to Unix Timestamp Pin
Anonymous29-Apr-05 22:22
Anonymous29-Apr-05 22:22 
GeneralRe: DateTime to Unix Timestamp Pin
Miros7929-Apr-05 22:24
Miros7929-Apr-05 22:24 
GeneralExample is incorrect Pin
David Palmer18-Apr-05 6:18
David Palmer18-Apr-05 6:18 
GeneralAddSeconds Pin
bsieber11-Apr-05 2:29
bsieber11-Apr-05 2:29 
GeneralRe: AddSeconds Pin
Miros7911-Apr-05 2:39
Miros7911-Apr-05 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.