Created
May 19, 2011 11:39
-
-
Save liddellj/980566 to your computer and use it in GitHub Desktop.
C# DateTime to Unix Timestamp Extension Method
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 class DateTimeExtensions | |
{ | |
public static long ToUnixTimestamp(this DateTime d) | |
{ | |
var duration = d - new DateTime(1970, 1, 1, 0, 0, 0); | |
return (long)duration.TotalSeconds; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.NET Framework 4.6 introduced
DateTimeOffset.ToUnixTimeSeconds
https://msdn.microsoft.com/en-us/library/system.datetimeoffset.tounixtimeseconds%28v=vs.110%29.aspx