Created
January 31, 2018 19:27
-
-
Save gmcelhanon/68793f6a3a2aded9f25659fbe5f3c95b to your computer and use it in GitHub Desktop.
Extension methods for converting .NET DateTime to/from UNIX time
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
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