As work requires, you can write a help document for yourself.
System. DateTime currentTime = new System. DateTime (); // instantiate a datetime object
Current year month day hour minute second currentTime = System. DateTime. Now;
Current Year int Year = currentTime. Year;
Current Month int Month = currentTime. Month;
Int Day = currentTime. Day;
Current Time int = currentTime. Hour;
Current Minute int Minute = currentTime. Minute;
Current Second int Second = currentTime. Second;
Current Millisecond int Millisecond = currentTime. Millisecond; (the variable can be in Chinese)
DateTime. Now. ToString (); // obtain the complete date and time of the current system time.
DateTime. Now. ToLongDateString (); // only display the date xxxx-xx, and the other is a long date.
DateTime. Now. ToShortDateString (); // only the date xxxx-xx is a short date.
DateTime. Now. Date. tow.datestring (); // Today
DateTime. Now. AddDays (-1). tow.datestring (); // Yesterday
DateTime. Now. AddDays (1). tow.datestring (); // tomorrow
Chinese date year month day time string strY = currentTime. ToString ("f"); // do not display seconds
Chinese Date and year string strYM = currentTime. ToString ("y ");
Chinese Date Month day string strMD = currentTime. ToString ("m ");
Current year month day format: 2003-9-23 string strYMD = currentTime. ToString ("d ");
Current Time Format: 14: 24 string strT = currentTime. ToString ("t ");
For more formats, see Appendix 1 and 2.
// This Week (note that every week starts from Sunday to Saturday)
DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek). ToShortDateString ();
DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek). ToShortDateString ();
// Last week (last week is the week minus 7 days)
DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). ToShortDateString ();
DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek)-7). ToShortDateString ();
// Next week (plus 7 days this week)
DateTime. Now. AddDays (Convert. ToDouble (0-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). ToShortDateString ();
DateTime. Now. AddDays (Convert. ToDouble (6-Convert. ToInt16 (DateTime. Now. DayOfWeek) + 7). ToShortDateString ();
// This month (the first day of this month is the first day of the month, and the last day is the second day of the month minus one day)
DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"; // The first day
DateTime. parse (DateTime. now. year. toString () + DateTime. now. month. toString () + "1 "). addMonths (1 ). addDays (-1 ). toShortDateString (); // last day
--------------------------------------------------------------------------------
Appendix 1: datetime type in tostring (), format settings
Format characters |
Association property/Description |
D |
ShortDatePattern |
D |
LongDatePattern |
F |
Complete date and time (long date and short time) |
F |
FullDateTimePattern (long date and long time) |
G |
Regular (short date and short time) |
G |
Regular (short date and long time) |
M, M |
MonthDayPattern |
R, R |
RFC1123Pattern |
S |
Use SortableDateTimePattern of the local time (based on ISO 8601) |
T |
ShortTimePattern |
T |
LongTimePattern |
U |
UniversalSortableDateTimePattern is used to display the format of common time |
U |
Use the complete date and time of the general time (long date and long time) |
Y, Y |
Y, Y YearMonthPattern |
Appendix 2: The following table lists the modes that can be merged to build a custom mode. These modes are case sensitive.
Format characters |
Association property/Description |
D |
A day in the month. A single-digit date has no leading zero. |
Dd |
A day in the month. A single-digit date has a leading zero. |
Ddd |
The abbreviation of a day in a week, which is defined in AbbreviatedDayNames. |
Dddd |
The full name of the day of the week, which is defined in DayNames. |
M |
Number of months. One-digit month does not have a leading zero. |
MM |
Number of months. A single-digit month has a leading zero. |
MMM |
The abbreviated name of a month, which is defined in AbbreviatedMonthNames. |
MMMM |
The complete name of the month, which is defined in MonthNames. |
Y |
Does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year that does not have the leading zero is displayed. |
Yy |
Does not contain the year of the epoch. If the year that does not contain the epoch is less than 10, the year with the leading zero is displayed. |
Yyyy |
This includes the four-digit year of the epoch. |
Gg |
Period or epoch. This mode is ignored if the date to be formatted does not have an associated period or epoch string. |
H |
The hour in 12-hour format. One-digit hours do not have a leading zero. |
Hh |
The hour in 12-hour format. The number of hours in a single digit has a leading zero. |
H |
The hour in the 24-hour format. One-digit hours do not have a leading zero. |
HH |
The hour in the 24-hour format. The number of hours in a single digit has a leading zero. |