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:
- 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:
- 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:
- private void Testdatetimelong () {
- Double doubledate = DateTime.Now.ToOADate ();
- DateTime thedate = datetime.fromoadate (doubledate);
- This.textBox1.Text = "";
- This.textBox1.AppendText ("Double Value of Now:" + doubledate.tostring () + "\ n");
- This.textBox1.AppendText ("DateTime from Double value:" + thedate.tostring () + "\ n");
- }
Operation Result:
- Double Value of now:37494.661541713
- DateTime from Double value:2002-8-26 15:52:37
(DateTime) Date type data converted to C # Long integer data