Obtain the same timestamp as the PHP function time () in C # [original]

Source: Internet
Author: User
Tags time zones

C # does not have the same time () timestamp function as PHP, but DateTime. Now. Ticks is used to calculate the time difference.

The value of this attribute is the number represented at an interval of January 1, 0001 milliseconds since midnight, January 1, 100.
In dotnet, DateTime. Now. Ticks can be used to obtain the exact time, measured in 10 ^-7 s.

After experiments, we found that

Long a = (DateTime. Now. Ticks-aa.Ticks)/10000000-8*60*60;
TbxUrl. Text = a. ToString ();

It takes 8*60*60 to get the same value as the time () function.
What's the problem? It is the difference between eight time zones.

DateTime aa = new DateTime (1970, 1, 1 );
MessageBox. Show ("1970" + aa. Ticks. ToString () + "now" + DateTime. Now. Ticks. ToString ());
Long a = (DateTime. Now. Ticks-aa.Ticks)/10000000-8*60*60;
TbxUrl. Text = a. ToString ();

At the same time, we found that DataTime in c # has an attribute! DateTime. UtcNow has considered the time zone issue.

OK ~ Use the following code.

DateTime timeStamp = new DateTime (, 1, 1); // obtain the timeStamp of January 1 ,.
Long a = (DateTime. UtcNow. Ticks-timeStamp.Ticks)/10000000; // note that there is a time zone problem here, it will take 8 hours to use now

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.