Click here to Skip to main content
Click here to Skip to main content

UNIX timestamp to System.DateTime

, 10 Apr 2005
Rate this:
Please Sign up or sign in to vote.
Converting UNIX timestamp to System.DateTime.


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.

// 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

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...


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


About the Author

Denmark Denmark
No Biography provided

Comments and Discussions

Generalummm actually... PinmemberJonathan Lathigee22-Jan-10 11:23 
GeneralJavascript-time and UNIX-Epoch-time start at the same Epoch but are measured with a different scale Pinmemberprotron1-Mar-11 10:54 
GeneralMy vote of 1 PinmemberDeceptor114-Jan-10 11:04 
Generalactually, none of you bitches are correct PinmemberEmil Müller4-Apr-08 12:25 
GeneralRe: actually, none of you bitches are correct PinmemberMikhail Tsennykh (devnoob)8-May-12 8:13 
QuestionIs this correct? Pinmembertobias4215-Oct-06 3:39 
GeneralVery Useful article PinmemberMudkiSekhon29-Dec-05 0:13 
GeneralCode sample not correct PinsussRiCkY8214-Jul-05 1:12 
As someone already noticed...
Because the timestamp is saved to UTC Time you should convert it back to the Local Time with
dateTime = dateTime.AddSeconds(tempTime).ToLocalTime();
this will give back the correct local time.
GeneralDateTime to Unix Timestamp PinmemberEiswuerfelJr29-Apr-05 8:02 
GeneralRe: DateTime to Unix Timestamp PinsussAnonymous29-Apr-05 23:22 
GeneralRe: DateTime to Unix Timestamp PinmemberMikkel Rostock29-Apr-05 23:24 
GeneralExample is incorrect PinmemberDavid Palmer18-Apr-05 7:18 
GeneralAddSeconds Pinmemberbsieber11-Apr-05 3:29 
GeneralRe: AddSeconds PinmemberMikkel Rostock11-Apr-05 3:39 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150305.1 | Last Updated 11 Apr 2005
Article Copyright 2005 by Miros79
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid