MFC control (10): Date time picker

Source: Internet
Author: User

In some cases, you may need to enter time information. there is a date time picker control in MFC to conveniently implement this function. it is found that the information of the MFC control is a bit messy. In toolbox, it is called date time picker. On the properties page, date-time control is displayed, and the corresponding class name becomes cdatetimectrl. of course, the names of different controls are not similar, so it will not cause confusion.

Speaking of time, we think of year, month, day (usually called date), hour, minute, second (usually called time). Of course, there are still milliseconds, which are not very common in the century.

You can make different settings for date-time control to select the date or time (however, you cannot set the date and time in a control at the same time, so if you need to set the information of the two, you must use two controls)

 

Select year, month, and day

// Drag the date time picker from toolbox to the page, and the properties in properties remain unchanged. format is the default short date, and use spin control is false.

// Bind a variable

Cdatetimectrl m_datectrl;

Ddx_control (PDX, idc_datetimepicker1, m_datectrl );

 

// The default display format is month/day/year. If you think it is not pleasing to the eye, you can change it to year/month/day.

// Anyway, yyyy indicates the year, MM indicates the month, and DD indicates the day. You can adjust the order by the way. in addition, the case cannot be fixed. here, the entire month is mm to distinguish it from mm that represents minutes.

 

M_datectrl.setformat (_ T ("yyyy/mm/DD "));

 

// When the page is opened, it is very convenient to select a date in the control. Now the next step is how to obtain the time information in the code.

// We can use the class ctime (a class in ATL, and ATL is also a class library like MFC. however, they are all integrated into vs. We don't need to distinguish who's class when using it)

 

Ctime m_date; // after the name is used, it only has time information. In fact, it can save both date and time information.

M_datectrl.gettime (m_date );//Get the time information in the control. It will be convenient to use later.

Int year = m_date.getyear ();

Int month = m_date.getmonth ();

Int day = m_date.getday ();

 

 

Select time

// Similarly, the date time picker control is used, but the attribute needs to be changed. Change the attribute format in properites to time and the user spin control to true.

// Of course, you can also choose not to change the format, and set it with setformat in the code.

Cdatetimectrl m_timectrl;

Ddx_control (PDX, idc_datetimepicker2, m_timectrl );

 

// By default, the time is displayed in the same format as the time settings on your operating system. Sometimes, only hours and minutes are displayed, but no seconds are displayed. You can set the display seconds in the code.

M_timectrl.setformat (_ T ("HH: mm: sstt "));

 

Obtain time information

Ctime m_time;

M_timectrl.gettime (m_time );

Int hour = m_time.gethour ();

Int minute = m_time.getminute ();

Int second = m_time.getsecond ();

 

Calculation of date and time

If you want to merge the date and time into the new ctime, it's also very easy.

Ctime m_datetime (year, month...); // you just need to input six parameters.

Ctime is a class in the old class library and has a small scope. Similar to coledatetime, ctime can be constructed by passing six parameters.

In addition, a useful function is used to obtain the current time.

Ctime time1 = ctime: getcurrenttime ();

 

In addition, we can add or subtract the date and time.

For example

Ctimespan timespan = m_date1-m_date2;

 

For coledatetime, the addition and subtraction results correspond to the coledatetimespan type.

 

 

 

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.