In the software development process, we often encounter problems that let users query according to date conditions. Typically, we will place two DateTimePicker controls to let users choose freely. However, if the user needs to query the data during the specified period, the date of the period is specified. If we can preset the commonly used standards to the software, it will make the user more convenient. Here is my solution:
///
/// Declaration Type enumeration
/// summary>
Public Enum Period {Day, Week, Month, Year};
///
/// Get the date of startup during the specified period
/// summary>
/// Type of time param>
/// Start date param>
/// End date param>
Public Static Void getPeriod (Period Period, Out DateTime Begindate,
Out datetime enddate
{
Int year = datetime.today.year;
Int month = datetime.today.month;
Switch (Period)
{
Case period.year: // year
Begindate = New datetime (Year, 1, 1);
Enddate = New datetime (Year, 12, 31);
Break;
Case Period.month: // month
Begindate = New datetime (year, month, 1);
Enddate = begindate.addmonths (1) .adddays (-1);
Break;
Case Period.Week: // Week
Int week = (int) datetime.today.dayofweek;
IF (week == 0) Week = 7; // Sunday
Begindate = DATETIME.TODAY.ADDDAYS (- (WEEK - 1));
Enddate = begindate.adddays (6);
Break;
Default: // day
Begindate = datetime.toDay;
Enddate = datetime.toDay;
Break;
}
}
//
Call example
DateTime Begindate, Enddate;
GetPeriod (Period.Year, Out BegindATE, OUT Enddate);