Wednesday, June 15, 2011

Convert Epoch Time to DateTime in C#

Me and my fellow developer encountered a scenario to convert epoch time to datetime. The solution can be found in stack overflow.
Code:
private void Form1_Load(object sender, EventArgs e)  
{  
     //long tms = 1308139229;  
     long tms = 1308143650;  
     DateTime dt = FromUnixTime(tms);  
     MessageBox.Show(dt.ToShortDateString());  
} 
 
public DateTime FromUnixTime(long unixTime)  
{  
   var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 
         DateTimeKind.Utc);  
   return epoch.AddSeconds(unixTime);  
}  

0 comments:

Post a Comment