Introduction to c datetime method application

  • 2020-05-07 20:21:23
  • OfStack

Along with the needs of the work, it is also to write a help document for yourself.
System. DateTime currentTime = new System. DateTime (); // instantiates an datetime object
currentTime= System. DateTime. Now;
Current year int =currentTime.Year;
Current month int = currentTime.Month;
Current day int =currentTime.Day;
Current int = currentTime. Hour;
Current score int score = currentTime.Minute;
Current seconds int seconds = currentTime.Second;
Current milliseconds int milliseconds = currentTime.Millisecond; (variables can be used in Chinese)
DateTime. Now. ToString (); // gets the full date and time of the current system time
DateTime. Now. ToLongDateString (); // only the date xxxx, xx, xx, 1 is a long date
DateTime. Now. ToShortDateString (); // only the date xxxx-xx-xx 1 is a short date
DateTime. Now. Date. ToShortDateString (); / / today
DateTime. Now. AddDays (1) ToShortDateString (); / / yesterday.
DateTime. Now. AddDays (1). ToShortDateString (); / /   tomorrow
string strY=currentTime.ToString("f"); // no seconds displayed
string strYM=currentTime.ToString("y");
string strMD=currentTime.ToString("m");
The current format is: 2003-9-23 string strYMD= currentTime.ToString ("d");
The current time format is: 14:24 string strT=currentTime.ToString("t");
For more formats see attached 1, 2.  

// this week (note that each week here starts from Sunday to week 6)
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 was this 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 (this week plus 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();
// this month (the 1st day of this month is the 1st day, and the last day is the 1st day of next month minus another day)
DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"; / / 1 day
DateTime. Parse (DateTime Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"). The AddMonths (1) AddDays (1) ToShortDateString (); // the last day
--------------------------------------------------------------------------------
Attachment 1: datetime type in tostring(), format format
format character   associated properties/description d   ShortDatePattern D   LongDatePattern   f   Full date and time (long date and short time) F   FullDateTimePattern (long date and long time) g   General (short date and short time) G   General (short date and long time) m, M   MonthDayPattern r, R     RFC1123Pattern s   SortableDateTimePattern using local time (based on ISO 8601) t   ShortTimePattern T   LongTimePattern u   UniversalSortableDateTimePattern is a format for displaying common times U   Full date and time using common time (long date and long time) y, Y   y, Y YearMonthPattern
Attachment 2: the following table lists patterns that can be combined to construct custom patterns. These patterns are case sensitive
format character associated properties/description d   A day of the month. A 1-digit date has no leading zero. dd   A day of the month. A 1-digit date has a leading zero. ddd   An abbreviation of a day of the week, as defined in AbbreviatedDayNames. dddd   The full name of a day of the week, as defined in DayNames. M   Monthly figures. 1 digit months have no leading zero. MM   Monthly figures. A 1-digit month has a leading zero. MMM   The abbreviated name of the month, as defined in AbbreviatedMonthNames. MMMM   The full name of the month, as defined in MonthNames. y   A year that does not contain an era. If the year without an era is less than 10, the year without a leading zero is shown. yy   A year that does not contain an era. If the year without an era is less than 10, the year with a leading zero is shown. yyyy   A year containing four digits of an era. gg   Period or epoch. If the date you want to format does not have an associated period or era string, the pattern is ignored. h   12 hours. The number of 1-digit hours has no leading zero. hh   12 hours. The number of 1-digit hours has a leading zero. H 24-hour hours. The number of 1-digit hours has no leading zero.   HH   24-hour hours. The number of 1-digit hours has a leading zero.

Related articles: