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 in C#. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may 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>