Z5 Concepts

C#.NET - Converting milliseconds since Epoch to DateTime

by on 14. September 2010 02:42

     Have you ever retrieved a long string of numbers for a datetime before? Chances are that this string of numbers is the number of milliseconds since Epoch, aka Unix Date. According to Wikipedia, Epoch or Unix date is "defined as the number of seconds elapsed since midnight proleptic Coordinated Universal Time (UTC) of January 1, 1970. Yes, believe it or not, this sometimes happens when trying to retrieve the datetime of some piece of data.

     C#.NET allows for you to convert this long string of milliseconds to retrieve a more user friendly date and time. Depending on Daylight Savings Time, the converted time may be off by an hour, which you can modify the code to add/subtract an hour if needed. You can see this in the code below.





    static string epochTime(string d)



        long e = long.Parse(d);

        DateTime dt = DateTime.Parse("1970-01-01").AddMilliseconds(e);

        dt = dt.AddHours(-4);   // This is to allow for specific time zones.

        d = dt.ToString();

        return d;





      If you have any questions or need help, feel free to e-mail me at dj@z5concepts.com.



Troubleshooting | Visual Studio 2005 | Visual Studio 2008 | Websites

Comments are closed

Theme by Z5 Concepts