Get a timestamp like PHP function time () in C #

Source: Internet
Author: User
Tags time zones

In C #, get the same timestamp as the PHP function time ()

There is no time () timestamp function like PHP in C #, but DateTime.Now.Ticks is used to calculate the difference.

The value of this property is the number when the time elapsed at 100-nanosecond intervals since 12:00 midnight, January 1, 01.
Dotnet with DateTime.Now.Ticks can get accurate time, Unit is 10^-7s.

After the experiment, we found

long a= (datetime.now.ticks-aa.ticks)/10000000-8*60*60;tbxurl.text=a.tostring ();

To subtract 8*60*60 is the same as the value given by the time () function.
What's the problem? Is exactly the difference between the 8 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 ();

Also found in C # inside Datatime has a property! Datetime.utcnow has considered the time zone problem.

ok~ Use the following code to

datetime timestamp=new datetime (1970,1,1); Get 1970-year timestamp long a= (datetime.utcnow.ticks-timestamp.ticks)/10000000; Notice there's a time zone problem, and now it's going to lose 8 hours .

UNIX timestamps and time formats convert each other

/// <summary>///convert the UNIX timestamp format to C # DateTime format///</summary>///<param name= "TimeStamp" > Timestamp < /param>///<returns>datetime </returns>public datetime GetTime (String timeStamp) {datetime Dtstart = Ti MeZone.CurrentTimeZone.ToLocalTime (New DateTime (1970, 1, 1)); Long ltime = long. Parse (TimeStamp + "0000000"); TimeSpan Tonow = new TimeSpan (Ltime); Return Dtstart.add (Tonow);}

<summary>///convert C # datetime format to UNIX timestamp format//</summary>///<param name= "Time" > Times </param> <returns>double</returns>public int Convertdatetimeint (System.DateTime time) {int intResult = 0; System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime (New System.DateTime (1970, 1, 1)); Intresult = (int) (time-starttime). TotalSeconds; return intresult;}

Get a timestamp like PHP function time () 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.