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 #