It's well documented here DateTimeOffset.ToUnixTimeMilliseconds Net Framework 4.6 or higher Use the method DateTimeOffset.ToUnixTimeMilliseconds() It returns the number of milliseconds that have elapsed since T00:00:00.000Z. The Unix epoch is also called Unix time, POSIX time, or Unix timestamp. This means that at midnight of January 1, 1970, Unix time was 0. The Unix epoch is the number of seconds that have elapsed since Januat midnight UTC time minus the leap seconds. If you dont want to create the epoch DateTime instance when moving from DateTime to epoch you can also do: public static long ToUnixTime(this DateTime date) ToUniversalTime will convert a Local (or Unspecified) DateTime to Utc. Return Convert.ToInt64((date.ToUniversalTime() - epoch).TotalSeconds) To allow for date's Kind being either Utc or Local, use ToUniversalTime: public static long ToUnixTime(this DateTime date) Note the comment below from CodesInChaos that the above FromUnixTime returns a DateTime with a Kind of Utc, which is fine, but the above ToUnixTime is much more suspect in that doesn't account for what kind of DateTime the given date is. Return Convert.ToInt64((date - epoch).TotalSeconds) Public static long ToUnixTime(this DateTime date) Var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc) With all credit to LukeH, I've put together some extension methods for easy use: public static DateTime FromUnixTime(this long unixTime)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |