Datetime.ticks Properties and Unix timestamp conversions in C #

Source: Internet
Author: User
1. Related Concepts

Datetime.ticks: Represents the number of 100 nanoseconds experienced since 12:00:00 midnight on January 1, 01, that is, the Ticks property is 100 nanoseconds (1Ticks = 0.0001 milliseconds).

Unix Timestamp: The number of seconds that have elapsed since January 1, 1970 (Midnight of Utc/gmt), regardless of leap seconds.

1 seconds = 1000 milliseconds

1 ms = 1000 Subtle

1 μs = 1000 nanoseconds

Therefore, 1 milliseconds = 10000 nanoseconds

Conversion of 2.DateTime variables to timestamps

In practice it is often necessary to be accurate to milliseconds, so the following two functions are used in "milliseconds", and to be converted to "seconds", you can divide or multiply by 1000.

/// <summary>/// 获取1970-01-01至dateTime的毫秒数/// </summary>public long GetTimestamp(DateTime dateTime){    DateTime dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0);    return (dateTime.Ticks - dt1970.Ticks) / 10000;}/// <summary>/// 根据时间戳timestamp(单位毫秒)计算日期/// </summary>public DateTime NewDate(long timestamp){    DateTime dt1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0);    long t = dt1970.Ticks + timestamp * 10000;    return new DateTime(t);}

Datetime.ticks Properties and Unix timestamp conversions in C #

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.