Comparison of ctime and systemtime can also be converted to the date format.

Source: Internet
Author: User

VC provides two types of date variables.

One is ctime. The disadvantage is that the year is only supported until 2038, and will not be supported in the future. If your project has a life cycle of 20 to 30 years, you can choose to use systemtime. this time function is used for comparison and conversion.

One is systemtime.

Comparison cannot be performed directly. Take a look at the first ctime.

Ctime timefrom;
M_datefrom.gettime (timefrom );

Ctime timeto;
M_dateto.gettime (timeto );

Ctimespan timespan = timeto-timefrom;
If (timespan. getdays () <0)
{
Afxmessagebox (_ T ("date illegal "));
Return;
}

The code will not be explained.

Convert to date format

Cstring datefrom = timefrom. Format (_ T ("% Y-% m-% d "));

Cstring dateto = timeto. Format (_ T ("% Y-% m-% d "));

In this way, you can query the data between two dates.

Let's look at the comparison of systemtime and convert it to the date format.

Two variables are also defined.


Systemtime timefrom, timeto;
M_datefrom.gettime (& timefrom );
M_dateto.gettime (& timeto );

But how can we compare these two times? After checking a lot of information, we need to convert them into ctime for comparison.

This is meaningless.

Next we will introduce a memory comparison method.

The memory size can be determined by comparing the two dates. You don't have to bother with conversion.

Int Re = memcmp (& timefrom, & timeto, sizeof (systemtime ));
If (Re> 0)
{
Afxmessagebox (_ T ("date illegal "));
Return;
}

If it is Re> 0, it indicates that the first selected date is larger than the second one.

If it is Re <0, it indicates that the first selected date is smaller than the second one.

If Re = 0, the first selected date is equal to the second one.

Let's take a look at how to convert systemtime to the date format XXXX-XX-xx

Let's take a look at the effect of several of his methods.

Varianttimetosystemtime (null, & timefrom );

In this case, no matter which day you choose, it will always be one day. 1899-12-30.

You can't add this one.

Coledatetime oletime;

Varianttimetosystemtime (oletime, & timefrom );

The running result is the same as that day.

Is there no solution?

Let's take a look at this definition.

Date;

Varianttimetosystemtime (date, & timefrom );

Cstring datefrom;
Datefrom. Format (_ T ("% 04u-% 02u-% 02u"), timefrom. wyear, timefrom. wmonth, timefrom. wday );

In this way, the format of XXXX-XX is displayed completely.





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.