C# Unix TimeStamp To DateTime

C#
Copy to clipboard
fablecode
  1. // universal time
  2. public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
  3. {
  4.         // Unix timestamp is seconds past epoch
  5.         var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  6.  
  7.         dateTime = dateTime.AddSeconds(unixTimeStamp)
  8.                 .ToUniversalTime(); // universal time
  9.  
  10.         return dateTime;
  11. }
  12.  
  13. // local time
  14. public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
  15. {
  16.         // Unix timestamp is seconds past epoch
  17.         var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  18.  
  19.         dtDateTime = dtDateTime.AddSeconds(unixTimeStamp)
  20.                 .ToLocalTime();  // local time
  21.  
  22.         return dateTime;
  23. }
  24.                
  25.  

    Leave a comment

     
    Visual verification
     

    Related books

    More InfoLearn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    Learn C# in One Day and Learn It Well: C# for Beginners with Hands-on Project (Learn Coding Fast with Hands-On Project) (Volume 3)
    More InfoC#: Programming Basics for Absolute Beginners (Step-By-Step C#) (Volume 1)
    C#: Programming Basics for Absolute Beginners (Step-By-Step C#) (Volume 1)
    More InfoC# Programming: THE NO-NONSENSE GUIDE: Learn C# Programming Within 12 Hours!
    C# Programming: THE NO-NONSENSE GUIDE: Learn C# Programming Within 12 Hours!
    More InfoThe C# Player's Guide (3rd Edition)
    The C# Player's Guide (3rd Edition)
    More InfoMicrosoft Visual C# Step by Step (8th Edition) (Developer Reference)
    Microsoft Visual C# Step by Step (8th Edition) (Developer Reference)
    More InfoThe C# Programming Yellow Book
    The C# Programming Yellow Book
    More InfoC# Programming in easy steps
    C# Programming in easy steps
    More InfoC# 6.0 and the .NET 4.6 Framework
    C# 6.0 and the .NET 4.6 Framework