Skip to content

Instantly share code, notes, and snippets.

@brentmaxwell
Created March 17, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentmaxwell/81f2e335a2da61db3746 to your computer and use it in GitHub Desktop.
Save brentmaxwell/81f2e335a2da61db3746 to your computer and use it in GitHub Desktop.
Extensions to convert Unix time to DateTime
namespace System
{
public static class UnixTimeExtensions
{
public static long ToUnixTime(this DateTime datetime)
{
return (long)(datetime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
}
public static DateTime FromUnixTime(long time)
{
return (new DateTime(1970, 1, 1, 0, 0, 0)).AddSeconds(time);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment