Skip to content

Instantly share code, notes, and snippets.

@zmij
Created March 13, 2015 06:46
Show Gist options
  • Save zmij/9f55eb6a539885f4d1ce to your computer and use it in GitHub Desktop.
Save zmij/9f55eb6a539885f4d1ce to your computer and use it in GitHub Desktop.
C# DateTime to UnixTime and back
/**
* 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