0

Possible Duplicate:
How to convert UNIX timestamp to DateTime and vice versa?

I use this code to get the format datetime of a given UNIX epoch

DateTime epoch=new DateTime(1970,1,1);
DateTime dt=new DateTime(1325506582751000+epoch.Ticks());

The output result is 1975/x/y

which is incorrect.

Is the epoch incorrectly defined ? How can I get the correct one from any given datetime data ?

Community
  • 1
  • 1
Adul Adula
  • 47
  • 6

1 Answers1

2

try this

public DateTime FromUnixTime(long unixTime)
{
    var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    return epoch.AddSeconds(unixTime);
}

or you can read this article.

John Woo
  • 258,903
  • 69
  • 498
  • 492