1

I'm having a few issues with converting a long Unix time to UTC Datetime in c#

    public static DateTime FromUnixTime(long time1) //EPOCH TO UTC
    {
        DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
        return epoch.AddMilliseconds(time1);
    }

For example I have a time: 1372250710901

http://www.epochconverter.com/ says:

Assuming that this timestamp is in milliseconds:
GMT: Wed, 26 Jun 2013 12:45:10 GMT

Which is correct, my response is 26/06/2013 12:06:10.901 which is incorrect.

I'm sure I've missed something fantastically obvious but I've spent a while trawling the forums to work it out and failed miserably so if anyone can help that would be fantastic.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • http://stackoverflow.com/questions/249760/how-to-convert-unix-timestamp-to-datetime-and-vice-versa – Gopesh Sharma Jun 27 '13 at 12:29
  • I think after adding MilliSeconds you have to convert it to localtime.. **.ToLocalTime()** – Gopesh Sharma Jun 27 '13 at 12:31
  • adding .ToLocalTime() just causes it to append 1 hour to GMT rather than UTC. In this case it's approximately 39mins out. Thanks for your input! I also made a slight mistake in the original post: the time I'm getting for 1372250710901 is 26/06/2013 12:06:10.901, I've adjusted the post. – user2527639 Jun 27 '13 at 12:38
  • r u by any chance passing seconds instead of milliseconds in **time1** variable? – Gopesh Sharma Jun 27 '13 at 12:45
  • Unfortunately not. But I think I've found the issue. Console.WriteLine("{0}",totalmil.ToString("dd/MM/yyyy HH:MM:ss.fff")); Managed to give a screwed up figure. Thank You for your help, I just needed to take a step back and look at the code properly! – user2527639 Jun 27 '13 at 12:53
  • Please consider contributing an answer to your own question. – O. Jones Oct 23 '14 at 19:14

0 Answers0