Created
February 20, 2015 16:39
-
-
Save kpespisa/e87059db1de761690b70 to your computer and use it in GitHub Desktop.
Convert to and from Epoch
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 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