(DateTime) Date type data converted to C # Long integer data

Source: Internet
Author: User

The introduction of date data into C # Long integer data, because the various databases on the definition of date type and processing is not the same, the various languages on the definition of Date data processing is different, because, I prefer to convert the date data to grow integer data and then save to the database.

Ad:51cto Net + 12th salon: The beauty of big data-how to drive user experience with data

C # language is still a common thing, here we mainly introduce the conversion of date data into C # Long integer data, including the description of the DateTime constructor and so on.

Transferred from: http://developer.51cto.com/art/200908/147684.htm

Date data converted to C # Long integer data

Why convert date-based data to C # Long integer data? There are many reasons for this, but personally, it is often used for database date storage. Because the definitions and processing of date types are different for various databases, the definition of date data is handled differently in various languages, because I prefer to convert the date data into the growth integer data and then save it to the database. Although you can also use strings to save, using strings also involves many problems, such as areas, and it requires more space than saving C # Long integer data.

Date data, when participating in C #, should also be converted to long-integer data. Its long value is the number of times elapsed since 12:00 midnight on January 1, 01, at 100-nanosecond intervals. This number is called Ticks (scale) in the DateTime of C #. The DateTime type has a long integer read-only attribute named Ticks, which holds the value. Thus, it is very simple to get a long value from a datatime type of data, just read the Ticks value of the Datatime object, such as:

    1. Long longdate = DateTime. Now.ticks;

The corresponding function for constructing datetime data from Long integer data is also provided in the constructor for datetime: DateTime (Long). Such as:

    1. DateTime thedate = new datetime (longdate);

But this is a problem for many VB6 programmers, because the date data inside the VB6 is represented by a Double type, which is only a date when converted to a long integer, and no time is available. How do you reconcile these two types of dates?

System.DateTime provides a double tooadate () and a static DateTime FromOADate (double) two functions to solve this problem. The former outputs the current object as the original double value, while the latter obtains a System.DateTime object from a double value. Examples are as follows:

    1. private void Testdatetimelong () {
    2. Double doubledate = DateTime.Now.ToOADate ();
    3. DateTime thedate = datetime.fromoadate (doubledate);
    4. This.textBox1.Text = "";
    5. This.textBox1.AppendText ("Double Value of Now:" + doubledate.tostring () + "\ n");
    6. This.textBox1.AppendText ("DateTime from Double value:" + thedate.tostring () + "\ n");
    7. }

Operation Result:

    1. Double Value of now:37494.661541713
    2. DateTime from Double value:2002-8-26 15:52:37

(DateTime) Date type data converted to C # Long integer data

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.