C # timestamp Conversion

Source: Internet
Author: User
Tags timestamp to date

UNIX timestamps are not used in. Net-based applications. UNIX timestamps are used when. Net Applications interact with other applications (such as PHP and Java. Once used in a project, when a user submits data through a web app and shares it with an Android app, the data can be real and valid only when the time interval is within one minute. Otherwise, the data will not be processed. In the development of Asp.net, the object is often serialized into JSON data. JS is spliced into HTML, and the date object is serialized into the following format: {"Date ": "\/date (1349839763373) \/"}. js cannot recognize it yet. In this case, consider converting a date to a Unix timestamp.

The conversion between the date and Unix timestamp in C # is as follows:

/// <Summary> /// convert a date to a Unix timestamp /// </Summary> /// <Param name = "datetime"> </param> /// <returns> </returns> Public static long datetimetounixtimestamp (datetime) {var start = new datetime (1970, 1, 1, 0, 0, 0, datetime. kind); Return convert. toint64 (datetime-Start ). totalseconds );} /// <summary> // convert UNIX timestamp to date /// </Summary> /// <Param name = "unixtimestamp"> timestamp (seconds) </param> // <returns> </returns> Public static datetime unixtimestamptodatetime (this datetime target, long timestamp) {var start = new datetime (1970, 1, 1, 0, 0, 0, target. kind); return start. addseconds (timestamp );}

Speaking of this date (January 1, 1970), the computing and display of the time of computers and some electronic devices are based on time (that is, 00:00:00 on January 1, Greenwich Mean Time. the offset of the, gregali calendar) is standard, and some people play to the British Greenwich Observatory as "the place of time ".

 

Appendix:

1. timestamp conversion for each language: http://www.epochconverter.com/

2. UNIX time introduction: http://en.wikipedia.org/wiki/Unix_time

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.