Click here to Skip to main content
Licence 
First Posted 10 Apr 2005
Views 100,235
Bookmarked 15 times

UNIX timestamp to System.DateTime

By | 10 Apr 2005 | Article
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.

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

About the Author

Miros79

Other

Denmark Denmark

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalummm actually... PinmemberJonathan Lathigee10:23 22 Jan '10  
GeneralJavascript-time and UNIX-Epoch-time start at the same Epoch but are measured with a different scale Pinmemberprotron9:54 1 Mar '11  
GeneralMy vote of 1 PinmemberDeceptor110:04 14 Jan '10  
Generalactually, none of you bitches are correct PinmemberEmil Müller11:25 4 Apr '08  
here goes nothing:
 
// UNIX timestamp to local time. The conversion also takes into account the local timezone and the daylight saving time rules.
 
static DateTime UNIXTimeToDateTime(double unixTime)
{
return new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(unixTime).ToLocalTime();
}
GeneralRe: actually, none of you bitches are correct PinmemberMikhail Tsennykh (devnoob)7:13 8 May '12  
QuestionIs this correct? Pinmembertobias4212:39 5 Oct '06  
GeneralVery Useful article PinmemberMudkiSekhon23:13 28 Dec '05  
GeneralCode sample not correct PinsussRiCkY820:12 14 Jul '05  
GeneralDateTime to Unix Timestamp PinmemberEiswuerfelJr7:02 29 Apr '05  
GeneralRe: DateTime to Unix Timestamp PinsussAnonymous22:22 29 Apr '05  
GeneralRe: DateTime to Unix Timestamp PinmemberMikkel Rostock22:24 29 Apr '05  
GeneralExample is incorrect PinmemberDavid Palmer6:18 18 Apr '05  
GeneralAddSeconds Pinmemberbsieber2:29 11 Apr '05  
GeneralRe: AddSeconds PinmemberMikkel Rostock2:39 11 Apr '05  

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.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 11 Apr 2005
Article Copyright 2005 by Miros79
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid