Created
March 13, 2015 06:46
-
-
Save zmij/9f55eb6a539885f4d1ce to your computer and use it in GitHub Desktop.
C# DateTime to UnixTime and back
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
/** | |
* DOES NOT take timezones into account, just simple calculations | |
*/ | |
namespace YourNamespace { | |
public static class ExtensionsClassName { | |
static readonly DateTime EPOCH = new DateTime (1970, 1, 1, 0, 0, 0); | |
public static double UnixTime(this DateTime date) | |
{ | |
return (date - EPOCH).TotalSeconds; | |
} | |
public static DateTime FromUnixTime(this double utime) | |
{ | |
return EPOCH.AddSeconds (utime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment