The timestamp is actually the number of seconds or milliseconds from the current time to the time you want to calculate from 0:00:00:00 on January 1, 1970 (converted to Beijing time is 08:00:00:00 on January 1, 1970)
Generally speaking: the timestamp we use is 10 digits in seconds, and 13 digits in milliseconds
1. Conversion between time time and second timestamp
1. Convert the time time to a second timestamp
DateTime time = DateTime.Now;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
TimeSpan ts = time - startTime;
var timestamp = Convert.ToInt64(ts.TotalSeconds);
Console.WriteLine(timestamp);
2. Convert the second timestamp to time
long timestamp=1629160713;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
var time = startTime.AddSeconds(timestamp);
2. Conversion between time time and millisecond timestamp
1. Convert time time to millisecond timestamp
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
var timestamp= Convert.ToInt64(ts2.TotalMilliseconds);
Console.WriteLine(timestamp);
2. Convert the millisecond timestamp to time
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
var time = startTime.AddMilliseconds(cc);
Note: UtcNow
The standard time zone is obtained, while TimeZone.CurrentTimeZone.ToLocalTime
the local time zone is obtained, so there are two ways to obtain the time difference
//当地时区
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
TimeSpan ts = DateTime.Now - startTime;
//标准时区
TimeSpan ts2 = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);