SoFunction
Updated on 2025-03-08

Introduction to the application of c# datetime method

As you need your work, you can also write a help document that you have left for yourself.
currentTime=new (); //Instantiate a datetime object
Current Time=;
current year int year=;
current month int month=;
current day int day=;
current time int time=;
Current point int point=;
current second int second =;
Current milliseconds int milliseconds =; (Variables are available in Chinese)
();//Get the current system time Complete date and time
();//Only display dates xxx, xx, xx, xx, xx, xx, xx, one is a long date
();// Only display dates xxxx-xx-xx one is a short date
();//today
(-1).ToShortDateString();//Yesterday
(1).ToShortDateString();//Tomorrow
Chinese Date Year, Month, Day, Time, and Minute string strY=("f"); //No seconds are displayed
Chinese Date Year Month string strYM=("y");
Chinese date Month day string strMD=("m");
The current year, month, and date format is: 2003-9-23 string strYMD=("d");
The current time-division format is: 14:24 string strT=("t");
See more formats Attached 1 and 2.

//This week (note that every week here starts from Sunday to Saturday)
(((0 - Convert.ToInt16()))).ToShortDateString();
(((6 - Convert.ToInt16()))).ToShortDateString();
//Last week (Last week is minus 7 days this week)
(((0 - Convert.ToInt16())) - 7).ToShortDateString();
(((6 - Convert.ToInt16())) - 7).ToShortDateString();
//Next week (plus 7 days this week)
(((0 - Convert.ToInt16())) + 7).ToShortDateString();
(((6 - Convert.ToInt16())) + 7).ToShortDateString();
//This month (The first day of this month is on the 1st, and the last day is to reduce one day on the 1st of the next month)
() + () + "1"; //The first day
(() + () + "1").AddMonths(1).AddDays(-1).ToShortDateString();//Last day
--------------------------------------------------------------------------------
Attachment 1: The format setting of datetime type in tostring() and format
Format characters Associated Attributes/Descriptions
d   ShortDatePattern
LongDatePattern 
Full date and time (long date and short time)
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
SortableDateTimePattern using local time (based on ISO 8601)
t   ShortTimePattern
T   LongTimePattern
u UniversalSortableDateTimePattern Format for displaying common time
Full date and time (long date and long time) using common time
y、Y  y、Y YearMonthPattern

Appendix 2: The following table lists the patterns that can be merged to construct custom patterns. These patterns are case sensitive
Format characters Associated Attributes/Descriptions
One day in the month. A single-digit date has no leading zeros.
dd   One day in the month. A single-digit date has a leading zero.
ddd   The abbreviation name for a day in the week is defined in AbbreviatedDayNames.
dddd  The full name of a day in the week, defined in DayNames.
M   Month numbers. A single-digit month has no leading zeros.
MM   Month numbers. A single-digit month has a leading zero.
MMM  The abbreviation name of the month, defined in AbbreviatedMonthNames.
MMMM  The full name of the month, defined in MonthNames.
y   Years that do not include epochs. If the year that does not contain an epoch is less than 10, a year without leading zeros is displayed.
yy  Years that do not include epochs. If the year without an epoch is less than 10, the year with leading zeros is displayed.
yyyy  Including four-digit years of the epoch.
gg  Period or Era. If the date you want to format does not have an associated epoch or epoch string, this pattern is ignored.
h   12-hour hours. A single-digit hour number has no leading zeros.
hh   12-hour hours. The number of hours in a single digit has leading zeros.
H 24-hour hours. A single-digit hour number has no leading zeros.
HH  24-hour hours. The number of hours in a single digit has leading zeros.