![]() |
Languages »
C# »
General
Intermediate
UNIX timestamp to System.DateTimeBy Mikkel RostockConverting UNIX timestamp to System.DateTime. |
C#, Windows, .NET, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
This article is a very simple example of how to convert a UNIX timestamp to a System.DateTime in Visual C#.
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.
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);
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...
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
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 |