Today, we have a need for time stamp conversion. I found the relevant code online for half a day and it was tested to be valid. I would like to take this note to share with you!
1. Time Stamp converted to C # format time
/// <Summary> // convert the timestamp to C # format time // </Summary> /// <Param name = "timestamp"> UNIX timestamp format </ param> // <returns> C # format time </returns> Public static datetime gettime (string timestamp) {datetime dtstart = timezone. currenttimezone. tolocaltime (New datetime (1970, 1, 1); long ltime = long. parse (timestamp + "0000000"); timespan tonow = new timespan (ltime); Return dtstart. add (tonow );}
2. Convert datetime to Unix Timestamp
/// <Summary> // convert the datetime format to the Unix timestamp format /// </Summary> /// <Param name = "time"> datetime time format </param> // <returns> UNIX timestamp format </returns> Public static int convertdatetimeint (system. datetime time) {system. datetime starttime = timezone. currenttimezone. tolocaltime (new system. datetime (1970, 1, 1); Return (INT) (time-starttime ). totalseconds ;}
Source