1. Take the last day of a month
Method 1: calculate the number of days and years of the month.+Month+The number of days that can be added. For example, the last day of the month is used.
Private void getlastdateformonth (datetime dtstart, out datetime dtend)
{
Int dtyear, dtmonth;
Dtstart = datetime. now;
Dtyear = dtstart. Year;
Dtmonth = dtstart. month;
Int monthcount = datetime. daysinmonth (dtyear, dtmonth );
Dtend = convert. todatetime (dtyear. tostring () + "-" + dtmonth. tostring () + "-" + monthcount );
}
Method 2: retrieve the first day of next month minus one day is the last day.
Private void getlastdateformonth (datetime dtstart, out datetime dtend)
{
Int dtyear, dtmonth;
Dtstart = datetime. Now. addmonths (1 );
Dtyear = dtstart. Year;
Dtmonth = dtstart. month;
Dtend = convert. todatetime (dtyear. tostring () + "-" + dtmonth. tostring () + "-" + "1"). adddays (-1 );
}
Ii. Calculation of time difference
Method 1: UseTimespanAnd also introducesTimespanUsage
Related attributes and functions
Add : With another Timespan Value addition.
Days: Returns Timespan Value.
Duration: Obtain Timespan .
Hours: Returns Timespan Value
Milliseconds: Returns the value in milliseconds. Timespan Value.
Minutes: Returns the value calculated in minutes. Timespan Value.
Negate: Returns the reverse Number of the current instance.
Seconds: Returns the value calculated in seconds. Timespan Value.
Subtract: Subtract another Timespan Value.
Ticks: Return Timespan Value Tick Number.
Totaldays: Return Timespan The number of days.
Totalhours: Return Timespan The number of hours.
Totalmilliseconds: Return Timespan The number of milliseconds.
Totalminutes: Return Timespan The number of minutes.
Totalseconds: Return Timespan The number of seconds.
Simple Example:
Datetime d1 = new datetime );
Datetime D2 = new datetime );
Timespan D3 = d2.subtract (D1 );
Lbtime. Text ="Difference:"
+ D3.days. tostring () +"Days"
+ D3.hours. tostring () +"Hours"
+ D3.minutes. tostring () +"Minutes"
+ D3.seconds. tostring () +"Seconds";
Method 2: UseSQLInDatediffFunction
Usage:Datediff(Datepart, Startdate, Enddate)
It can help you get the time difference in various forms you want, such as how many days, how many hours, and how many minutes are separated. The specific format is as follows:
Date |
Abbreviations |
Year |
YY, yyyy |
Quarter |
QQ, Q |
Month |
Mm, m |
Dayofyear |
Dy, y |
Day |
DD, d |
Week |
Wk, WW |
Hour |
HH |
Minute |
Mi, n |
Second |
SS, S |
Millisecond |
MS |
For example:Datediff (MI, dtoptime, dtend)The total number of minutes of time difference between them can be retrieved, which has been converted for you. It is particularly useful for specifying the unit, namely, hour, minute, and second.