C # DateTime and timestamp conversion,
C # convert DateTime to timestamp, including the JavaScript Timestamp and Unix timestamp.
1. What is a timestamp?
First, you need to know the difference between the timestamp of JavaScript and Unix:
JavaScript timestamp: refers to the period from GMT, January 1, January 01, 1970 to UTC (GMT, January 1, January 01, 1970 ).Total milliseconds.
Unix timestamp: refers to the period from GMT, January 1, January 01, 1970 to UTC (GMT, January 1, January 01, 1970 ).Total seconds.
It can be seen that the JavaScript TimestampTotal millisecondsThe Unix timestamp isTotal seconds.
For example, for 12:30:00, convert to JavaScript timestamp 1478147400000; convert to Unix timestamp 1478147400.
2. JavaScript timestamp mutual conversion 2.1 C # DateTime conversion to JavaScript Timestamp
System. dateTime startTime = TimeZone. currentTimeZone. toLocalTime (new System. dateTime (1970, 1, 1); // long timeStamp = (long) (DateTime. now-startTime ). totalMilliseconds; // Number of milliseconds in difference System. console. writeLine (timeStamp );
2.2 convert JavaScript timestamps to C # DateTime
Long jsTimeStamp = 1478169023479; System. dateTime startTime = TimeZone. currentTimeZone. toLocalTime (new System. dateTime (1970, 1, 1); // The local time zone DateTime dt = startTime. addMilliseconds (jsTimeStamp); System. console. writeLine (dt. toString ("yyyy/MM/dd HH: mm: ss: ffff "));
3. Unix timestamp mutual conversion 3.1 C # DateTime conversion to Unix Timestamp
System. dateTime startTime = TimeZone. currentTimeZone. toLocalTime (new System. dateTime (1970, 1, 1); // long timeStamp = (long) (DateTime. now-startTime ). totalSeconds; // The number of seconds for the difference. console. writeLine (timeStamp );
3.2 convert Unix timestamp to C # DateTime
Long unixTimeStamp = 1478162177; System. dateTime startTime = TimeZone. currentTimeZone. toLocalTime (new System. dateTime (1970, 1, 1); // The local time zone DateTime dt = startTime. addSeconds (unixTimeStamp); System. console. writeLine (dt. toString ("yyyy/MM/dd HH: mm: ss: ffff "));
====================================== Series of articles ==============================================
This article: 1.4 C # DateTime and timestamp Conversion
C # Article navigation