Get current Time DateTime dt = DateTime.Now;
Into strings DateTime dt = DateTime.Now;
String str = dt. ToString (); This is the most direct method of conversion.
String str2 = dt. ToString ("Yyyy-mm-dd HH:mm:ss"); yyyy, MM, DD are format patterns, case sensitive
DateTime's ToString format pattern is case sensitive, please note case, each pattern can be combined with each other, the common pattern is as follows: YYYY years, four-digit number.
YY years, two-digit representation. MM month, less than 10 o'clock, with leading zeros.
M month, less than 10 o'clock, no leading zero. DD Day, less than 10 o'clock, with leading zeros.
D-Day, less than 10 o'clock, without leading zeros. HH time (24 hours), less than 10 o'clock, has a leading zero.
H (24 hours), less than 10 o'clock, without leading zeros. HH Time (12 hours), less than 10 o'clock, has a leading zero.
H (12 hours), less than 10 o'clock, without leading zeros. MM points. is less than 10 o'clock with a leading zero.
M points. is less than 10 o'clock and has no leading zeros. SS seconds. is less than 10 o'clock with a leading zero.
s seconds. is less than 10 o'clock and has no leading zeros.
get year, month, day, hour, minute, second, etc. DateTime dt = DateTime.Now;
int y = dt. year; Years
int m = dt. Month; Month
int d = dt. Day; Days, note that day is not date
int h = dt. Hour; When
int n = dt. Minute; Part
int s = dt. Second; Seconds
int ms = Dt.millisecond; Milliseconds
Long T = dt. Ticks; A number that represents the time, and note that the type is long
Time Comparison DateTime dt1 = DateTime.Now;
DateTime DT2 = dt1. Addyears (3);
int ct1 = dt1. CompareTo (DT2); Dt1 earlier than DT2, return-1
int ct2 = DT2. CompareTo (DT1); DT2 later than DT1, return 1
int ct3 = DT2. CompareTo (DT2); DT2 is equal to DT2 and returns 0
DateTime dt1 = DateTime.Now;
DateTime DT2 = dt1. Addyears (3);
BOOL B = dt1. Equals (DT2); DT1 is not equal to DT2 and returns false
time plus minus DateTime dt = DateTime.Now;
DT = dt. Addyears (1); Plus 1 years
DT = dt. Addmonths (-1); Minus January
DT = dt. AddDays (13); Add 13th
DT = dt. AddHours (1); When
DT = dt. AddMinutes (1); Part
DT = dt. AddSeconds (1); Seconds
DT = dt. Addmilliseconds (1); Milliseconds
DT = dt. Addticks (1); Ticket, representing the current time with numbers
The parameter is positive, the parameter is negative, and the minus is indicated. Pay attention to spelling. The function should return a value, such as: dt = dt. Addyears (1), instead of: dt. Addyears (1).
Another method is ADD, whose syntax is: DateTime datetime.add (TimeSpan valuetype);
For TimeSpan, please continue reading this article.
Time Subtraction Operation
The previous time plus minus is a time to add minus, here is two time to reduce the operation (with-), the return of the result type is TimeSpan. DateTime dt1 = DateTime.Now;
DateTime DT2 = dt1. AddDays (3);
TimeSpan ts = DT1-DT2;
The TimeSpan properties days, Hours, Minutes, Seconds, milliseconds, tickets, respectively, return the difference in number, hours, fractions, seconds, milliseconds, and tickets. There are positive and negative points.
The Timsspan properties Totaldays, Totalhours, Totalminutes, TotalSeconds, TotalMilliseconds, respectively, return the difference in days, hours, minutes, seconds, and milliseconds. There are positive and negative points.
For example: Dt1 than DT2 3 days early, according to the above procedure, then:
Days for -3,hours for 0;
Totaldays is -3,totalhours-72 (3 days is 72 hours).
Original address: http://www.cnblogs.com/dudu837/archive/2009/04/28/1445467.html