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.