Skip to content

Instantly share code, notes, and snippets.

@gmcelhanon
Created January 31, 2018 19:27
Show Gist options
  • Save gmcelhanon/68793f6a3a2aded9f25659fbe5f3c95b to your computer and use it in GitHub Desktop.
Save gmcelhanon/68793f6a3a2aded9f25659fbe5f3c95b to your computer and use it in GitHub Desktop.
Extension methods for converting .NET DateTime to/from UNIX time
public static DateTime FromUnixTime(this long unixTime)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return epoch.AddSeconds(unixTime);
}
public static long ToUnixTime(this DateTime date)
{
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
return Convert.ToInt64((date - epoch).TotalSeconds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment