From UnixTime to DateTime and back

Here are a couple of functions that allow you to convert from UnixTime to DateTime and back:


public class Util {
 private static DateTime UnixTime
 {
  get { return new DateTime(1970, 1, 1); }
 }

 public static DateTime FromUnixTime( double unixTime )
 {
  return UnixTime.AddSeconds( unixTime );
 }

 public static double ToUnixTime( DateTime dateTime )
 {
  TimeSpan timeSpan = dateTime - UnixTime;
  return timeSpan.TotalSeconds;
 }
}
 

This entry was posted on Wednesday, October 4th, 2006 at 16:55 and is filed under C#. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed.