C # DateTime and timestamp conversion,

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.