TimeSpan is a good time computing class.
Eg1.DateTime dt1 = DateTime. Now;
DateTime dt2 = DateTime. Now. AddDays (-7 );
TimeSpan ts = dt1-dt2;
Int days = ts. Days; // The difference between dt1 and dt2
Eg2.
// Edit whether the month is the same as the month of system logon.
If (Convert. ToDateTime (DateTime. Now. tow.datestring (). Month = Convert. ToDateTime (this. GetQueryString ("sdate"). Month)
{
TimeSpan ts = Convert. ToDateTime (DateTime. Now. tow.datestring ()-Convert. ToDateTime (this. GetQueryString ("sdate "));
IDays = ts. Days;
// You can edit the three days before logging on to the system.
If (iDays <4)
{
Submiter submit = new Submiter (this, "tt ");
This. iseretail. SelectedValue = this. GetQueryString ("rid ");
This.txt Time. Text = this. GetQueryString ("sdate"). ToString ();
This. InputSelect1.WhereCondition = "gxlshd = '" + this. GetQueryString ("rid") + "'";
If (this. GetQueryString ("workid ")! = "")
This. InputSelect1.SelectedValue = this. GetQueryString ("workid ");
Submit. InitInsertMode ();
SetDataGrid ();
}
Eg3.C # calculates the difference between the two time points.
/// <Summary>
/// Calculate the interval between two dates
/// </Summary>
/// <Param name = "DateTime1"> first date and time </param>
/// <Param name = "DateTime2"> Second Date and time </param>
/// <Returns> </returns>
Private string DateDiff (DateTime DateTime1, DateTime DateTime2)
{
String dateDiff = null;
TimeSpan ts1 = new TimeSpan (DateTime1.Ticks );
TimeSpan ts2 = new TimeSpan (DateTime2.Ticks );
TimeSpan ts = ts1.Subtract (ts2). Duration ();
DateDiff = ts. Days. ToString () + "day"
+ Ts. Hours. ToString () + "hour"
+ Ts. Minutes. ToString () + "Minute"
+ Ts. Seconds. ToString () + "Seconds ";
Return dateDiff;
} Note:
1. The DateTime Value Type represents a specific date time between 00:00:00 on January 1, January 1, 0001 AD and on January 1, December 31, 9999 AD. Therefore, you can use the DateTime value type to describe any time within the expected range. A DateTime value represents a specific time.
2. the TimeSpan value contains many attributes and methods for accessing or processing a TimeSpan value.
The following list covers some of them:
Add: adds the value to another TimeSpan value.
Days: returns the TimeSpan value calculated by the number of Days.
Duration: gets the absolute value of TimeSpan.
Hours: returns the hourly TimeSpan value.
Milliseconds: returns the TimeSpan value in Milliseconds.
Minutes: returns the TimeSpan value calculated in Minutes.
Negate: returns the opposite number of the current instance.
Seconds: returns the TimeSpan value calculated in Seconds.
Subtract: Subtract another TimeSpan value from it.
Ticks: the number of tick values returned for TimeSpan.
TotalDays: returns the number of days indicated by the TimeSpan value.
TotalHours: returns the number of hours represented by the TimeSpan value.
TotalMilliseconds: the number of milliseconds in which the TimeSpan value is returned.
TotalMinutes: returns the number of minutes represented by the TimeSpan value.
TotalSeconds: returns the number of seconds represented by the TimeSpan value.