The time stamp is actually the number of seconds or milliseconds you need to count the time from 00:00:00 on January 1, 1970 (00:00:00 on January 1, 1970). Generally speaking, we use 10 bits to seconds and 13 bits to milliseconds

1. Convert time to second timestamp

DateTime time = DateTime.Now; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // Local time zone TimeSpan ts = time-startTime; var timestamp = Convert.ToInt64(ts.TotalSeconds); Console.WriteLine(timestamp);Copy the code

2. Convert the second timestamp to time

long timestamp=1629160713; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // Local time zone var time = starttime.addseconds (timestamp);Copy the code

1. Convert 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);
Copy the code

2. Convert the millisecond timestamp to time

System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); / var/local time zone time = startTime. AddMilliseconds (cc);Copy the code

Note: UtcNow access is the standard time zone, and TimeZone. CurrentTimeZone. ToLocalTime access is the local time zone, so there will be two kinds of writing for time difference

/ / System. The local time zone DateTime startTime = TimeZone. CurrentTimeZone. ToLocalTime (new System. A DateTime (1970, 1, 1)); // Local time zone TimeSpan ts = datetime. now-starttime;Copy the code
TimeSpan ts2 = datetime. UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);Copy the code