c sharp common date time function

  • 2020-05-05 11:50:07
  • OfStack

--DateTime digital
System.DateTime currentTime=new System.DateTime();
1.1 take the minutes and seconds
of the current year, month and day currentTime=System.DateTime.Now;
1.2 take the current year
int years = currentTime. Year;
1.3 take the current month
int month = currentTime. Month;
1.4 take the current day
int day = currentTime. Day;
1.5 take the current time
When int = currentTime. Hour;
1.6 take the current score
int points = currentTime. Minute;
1.7 take the current second
int seconds = currentTime. Second;
1.8 take the current millisecond
int ms = currentTime. Millisecond;
(variables available in Chinese)
1.9 Chinese date display --
(year, month, day string strY = currentTime. ToString (" f "); //
in seconds is not displayed 1.10 Chinese date display _ date
string strYM=currentTime.ToString("y");
1.11 Chinese date display _ month date
string strMD=currentTime.ToString("m");
1.12 take the current year, month and day, and the format is: 2003-9-23
string strYMD=currentTime.ToString("d");
1.13 takes the current time, and the format is: 14:24
string strT=currentTime.ToString("t");
/ /
today DateTime.Now.Date.ToShortDateString();
// yesterday, that's today's date minus one
DateTime.Now.AddDays(-1).ToShortDateString();
// tomorrow, same thing, plus one
DateTime.Now.AddDays(1).ToShortDateString();
// this week) to know the first day of the week you need to know what day of the week it is to know that the first day of the week was a few days ago DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
// if you still don't understand, you should understand
by looking at the Chinese way of showing the day of the week // since DayOfWeek returns a number of days of the week, we have to convert it into Chinese characters for us to read, some people may use switch to compare one by one, in fact, there is not so much trouble string[] string[] {" Sunday ", "Monday "," Tuesday ", "Wednesday "," Thursday ", "Friday "," Saturday "};
Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
// last week, same thing, a week is 7 days, last week is this week minus 7 days, same thing next 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();
/ /
next 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, many people will say that the first day of this month must be the 1st, and the last day will be the 1st of next month minus another day. And of course that's true for
//
DateTime. Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"; // day 1
DateTime. Parse (DateTime Now. Year. ToString () + DateTime. Now. Month. ToString () + "1"). The AddMonths (1) AddDays (1) ToShortDateString (); // the last day

Related articles: