(1).Compare the size of 2 DateTimes
Copy the codeThe code is as follows:
DateTime dt1 = ("2010/11/25 20:53:43");
DateTime dt2 = ("2010/11/26 19:24:53");
int i = (dt1, dt2);
The final result of i is -1...so dt1<dt2..when dt1=dt2 i=0..when dt1>dt2 i=1
(2). Calculate the difference between two DateTimes
Copy the codeThe code is as follows:
DateTime dt1 = ("2010/11/25 20:53:43");
DateTime dt2 = ("2010/11/25 20:53:43");
DateTime dt3 = ("2010/11/25 20:53:43");
TimeSpan diff1 = (dt1);
DateTime diff2 = (dt1);
TimeSpan diff3 = dt2 - dt3;
DateTime diff4 = dt1 - dt2;
The result is:
Copy the codeThe code is as follows:
diff1="185.14:47:00";(185 days, 14 hours, 47 minutes, 0 seconds)
diff2="1996/4/9 17:55:00";
diff3="55.04:20:00";(55 days, 4 hours, 20 minutes, 0 seconds)
diff4="1996/4/9 17:55:00";
(3). Calculate the date after subtracting or adding a date to a certain number of days
Copy the codeThe code is as follows:
TimeSpan ts = new TimeSpan(40, 0, 0, 0);
DateTime dt1 = (ts);
DateTime dt2 = (ts);
string str1 = () + "Day ago" + ("yyyyy year MM month dd day");
string str2 = () + "The queen is" + ("yyyy year MM month dd day");
The result is:
Copy the codeThe code is as follows:
str1="40 days ago was October 16, 2010";
str2="40 days later is January 4, 2011";
PS: (calculated date is November 25, 2010)