Thursday, August 16, 2012

c# - Converting TimeStamp to DateTime

Ever come across timestamps represented by large integer value? For example like this 1344938468,1344938468416? This is timestamp from Unix based system and represents the number of seconds/milliseconds elapsed since the Unix Epoch January 1st, 1970

Timestamp Seconds to DateTime


try
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);//Unix Epoch on January 1st, 1970
    Console.WriteLine("Converting timestamp in Seconds");
    Console.WriteLine(origin.AddSeconds(1344938468));
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message);
 
















Timestamp Milliseconds to DateTime


try
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);//Unix Epoch on January 1st, 1970
    Console.WriteLine("Converting timestamp in MilliSeconds");
    Console.WriteLine(origin.AddMilliseconds(1344938468416));
}
catch(Exception ex)
{
    Console.WriteLine(ex.Message);
}


      

1 comment:

Anonymous said...

Thanks a lot, it resolved my problem