Skip to content

Instantly share code, notes, and snippets.

@kpespisa
Created February 20, 2015 16:39
Show Gist options
  • Save kpespisa/e87059db1de761690b70 to your computer and use it in GitHub Desktop.
Save kpespisa/e87059db1de761690b70 to your computer and use it in GitHub Desktop.
Convert to and from Epoch
public static class DateTimeHelper
{
private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime UnixTimeToDateTime(string text)
{
double seconds = double.Parse(text, CultureInfo.InvariantCulture);
var time = Epoch.AddSeconds(seconds);
return time;
}
public static double DateTimeToUnixTime(DateTime date)
{
TimeSpan diff = date - Epoch;
return Math.Floor(diff.TotalSeconds);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment