_c# tutorial on system time and UNIX timestamp conversion in C #

Source: Internet
Author: User

In the project development process, sometimes encounter different programs to call each other data, the data will inevitably contain time, For example, asp.net call PHP, involved in time to do some processing, PHP program in general access are Unix time, unlike ASP.net storage is the month and a half minutes, so you need to do a transformation, the code is as follows:

First, convert system time to UNIX timestamp

Copy Code code as follows:
DateTime Dtstart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970,1,1));
DateTime Dtnow = DateTime.Parse (DateTime.Now.ToString ());
TimeSpan Tonow = dtnow.subtract (Dtstart);
String timeStamp = ToNow.Ticks.ToString ();
TimeStamp = timestamp.substring (0,timestamp.length-7);

Second, the Unix time stamp into the system time

Copy Code code as follows:
String timeStamp = "1288368000";
DateTime Dtstart = TimeZone.CurrentTimeZone.ToLocalTime (new DateTime (1970,1,1));
Long ltime = long. Parse (TimeStamp + "0000000");
TimeSpan Tonow = new TimeSpan (ltime);
DateTime Dtresult = Dtstart.add (Tonow);

These two methods are relatively simple, you can encapsulate them into static functions, call directly.

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.