Date-type data converted to C # Long integer data

Source: Internet
Author: User
Tags datetime integer

C # language is still more common, here we mainly introduce date-type data into C # Long integer data, including the introduction of DateTime constructors, and so on.

Why convert date-type data to C # Long integer data? There are many reasons for this, but personally, it is often used for the date store of the database. Since the definitions and processing of date types vary from one database to another, the definitions of date-type data are handled differently in various languages, because I would rather convert the date-type data into the database. Although you can also use strings to save, using strings can also involve many issues, such as zones, and it requires more space than saving C # Long integer data.

Date-type data, in C # 's participation in the operation, it should also be converted to long integer data to calculate. Its long value is the number that is represented by a 100-nanosecond interval of time since 12:00 midnight, January 1, 01. This number is called Ticks (scale) in the DateTime of C #. The DateTime type has a long, read-only property named Ticks that holds the value. Thus, it is very easy to get a long value from a datatime type of data, just read out the Ticks value of the Datatime object, such as:

long longDate = DateTime.Now.Ticks; 

The DateTime constructor also provides a corresponding function to construct the datetime data from the Long Integer data: datetime (Long). Such as:

DateTime theDate = new DateTime(longDate);

But that's a problem for a lot of VB6 programmers, because the date-type data in VB6 is represented by a Double, which, when converted to a long integer, gets only the date, not the time. How do you reconcile both 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 by the original double value, and 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");
}

Run Result:

Double value of now: 37494.661541713
DateTime from double value: 2002-8-26 15:52:37

Related Article

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.