Created
March 17, 2015 19:18
-
-
Save brentmaxwell/81f2e335a2da61db3746 to your computer and use it in GitHub Desktop.
Extensions to convert Unix time to DateTime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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