C # datetime and Timestamp conversions

Source: Internet
Author: User

Original: C # datetime and Timestamp conversions

C # datetime-to-timestamp conversions, including JavaScript timestamps and Unix timestamps.

1. What is a timestamp

The first thing to know is the difference between JavaScript and UNIX timestamps:

JavaScript Timestamp: GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) up to now the total number of milliseconds .

Unix Timestamp: GMT January 01, 1970 00:00 00 seconds (Beijing time January 01, 1970 08:00 00 seconds) up to now the total number of seconds .

You can see the total number of milliseconds for the JavaScript timestamp, and the Unix timestamp is the total number of seconds .

Like the same thing. 2016/11/03 12:30:00, converted to a JavaScript timestamp of 1478147400000, and converted to a Unix timestamp of 1478147400.

2. JavaScript timestamps Convert 2.1 C # datetime to JavaScript timestamp
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1)); local time zone long TimeStamp = (long) (Datetime.now-starttime). TotalMilliseconds; Number of milliseconds System.Console.WriteLine (TimeStamp);

2.2 JavaScript timestamp conversion to C # DateTime
Long jstimestamp = 1478169023479; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1)); local time zone DateTime dt = starttime.addmilliseconds (Jstimestamp); System.Console.WriteLine (dt. ToString ("Yyyy/mm/dd HH:mm:ss:ffff"));

3. UNIX timestamps convert 3.1 C # datetime to UNIX timestamp
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1)); local time zone long TimeStamp = (long) (Datetime.now-starttime). TotalSeconds; Number of seconds System.Console.WriteLine (TimeStamp);

3.2 Unix timestamp conversion to C # DateTime
Long unixtimestamp = 1478162177; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1)); local time zone DateTime dt = starttime.addseconds (Unixtimestamp); System.Console.WriteLine (dt. ToString ("Yyyy/mm/dd HH:mm:ss:ffff"));

================================== Series article ==========================================

This article: 1.4 C # datetime and Timestamp conversions

C # Article Navigation

C # datetime and Timestamp conversions

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.