Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 109,580 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,558 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Convert UNIX Timestamp to System.DateTime

This is a snippet to convert a UNIX timestamp to a readable System.DateTime value

Submitted By: PsychoCoder
Actions:
Rating:
Views: 942

Language: C#

Last Modified: June 30, 2008
Instructions: Pass the timestamp you wish to convert and the method will return the Datetime value for it

Snippet


  1. /// <summary>
  2. /// method for converting a UNIX timestamp to a regular
  3. /// System.DateTime value (and also to the current local time)
  4. /// </summary>
  5. /// <param name="timestamp">value to be converted</param>
  6. /// <returns>converted DateTime in string format</returns>
  7. private DateTime ConvertTimestamp(double timestamp)
  8. {
  9.     //create a new DateTime value based on the Unix Epoch
  10.     DateTime converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  11.  
  12.     //add the timestamp to the value
  13.     converted.AddSeconds(timestamp);
  14.  
  15.     //return the value in string format
  16.     return converted.ToLocalTime();
  17. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month