Click here to Skip to main content
6,292,426 members and growing! (10,176 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

UNIX timestamp to System.DateTime

By Mikkel Rostock

Converting UNIX timestamp to System.DateTime.
C#, Windows, .NET, Visual Studio, Dev
Posted:10 Apr 2005
Views:60,843
Bookmarked:11 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
14 votes for this article.
Popularity: 3.03 Rating: 2.64 out of 5
6 votes, 42.9%
1
2 votes, 14.3%
2

3
2 votes, 14.3%
4
4 votes, 28.6%
5

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

Mikkel Rostock


Member

Occupation: Web Developer
Location: Denmark Denmark

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
Generalactually, none of you bitches are correct PinmemberEmil Müller12:25 4 Apr '08  
GeneralIs this correct? Pinmembertobias4213:39 5 Oct '06  
GeneralVery Useful article PinmemberMudkiSekhon0:13 29 Dec '05  
GeneralCode sample not correct PinsussRiCkY821:12 14 Jul '05  
GeneralDateTime to Unix Timestamp PinmemberEiswuerfelJr8:02 29 Apr '05  
GeneralRe: DateTime to Unix Timestamp PinsussAnonymous23:22 29 Apr '05  
GeneralRe: DateTime to Unix Timestamp PinmemberMikkel Rostock23:24 29 Apr '05  
GeneralExample is incorrect PinmemberDavid Palmer7:18 18 Apr '05  
GeneralAddSeconds Pinmemberbsieber3:29 11 Apr '05  
GeneralRe: AddSeconds PinmemberMikkel Rostock3:39 11 Apr '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 10 Apr 2005
Editor: Rinish Biju
Copyright 2005 by Mikkel Rostock
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project