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;
 }
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>