1. Related Concepts
Datetime.ticks: Represents the number of 100 nanoseconds experienced since 12:00:00 midnight on January 1, 01, that is, the Ticks property is 100 nanoseconds (1Ticks = 0.0001 milliseconds).
Unix Timestamp: The number of seconds that have elapsed since January 1, 1970 (Midnight of Utc/gmt), regardless of leap seconds.
1 seconds = 1000 milliseconds
1 ms = 1000 Subtle
1 μs = 1000 nanoseconds
Therefore, 1 milliseconds = 10000 nanoseconds
Conversion of 2.DateTime variables to timestamps
In practice it is often necessary to be accurate to milliseconds, so the following two functions are used in "milliseconds", and to be converted to "seconds", you can divide or multiply by 1000.
/// <summary>/// 获取1970-01-01至dateTime的毫秒数/// </summary>public long GetTimestamp(DateTime dateTime){ DateTime dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0); return (dateTime.Ticks - dt1970.Ticks) / 10000;}/// <summary>/// 根据时间戳timestamp(单位毫秒)计算日期/// </summary>public DateTime NewDate(long timestamp){ DateTime dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0); long t = dt1970.Ticks + timestamp * 10000; return new DateTime(t);}
Datetime.ticks Properties and Unix timestamp conversions in C #