Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common date-time methods in C #

Shulou Source: shulou.com Published: 2022-05-31 20:01:06 10月17日 Update

This article mainly explains "what are the common date and time methods of C#". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "what are the common date and time methods of C#"!

January and Month English Abbreviations

DateTime dt = DateTime.Now;string MM = dt.AddMonths(-1).ToString("MMM", new System.Globalization.CultureInfo("en-us"));//Month English abbreviation: Jul

First and last day of the month

DateTime ThisMonth_Frist = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date;DateTime ThisMOnth_Last = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1);

Third, the first and last day of last month

DateTime Today = DateTime.Today;//DateTime ThisMonth = new DateTime(Today.Year, Today.Month, 1);//DateTime LastMonth_First = ThisMonth.AddMonths(-1);//DateTime LastMonth_Last = ThisMonth.AddDays(-1);//DateTime Last Day of Last Month

4. Days of the week

int daysInWeek1 = (int)DateTime.Now.DayOfWeek;//Note: Week, DayTime returns 0 here, int daysInWeek2 = (int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek;//the day of the week, note: Sunday is 0

V. Weeks of the month

int a = WeekOfMonth(DateTime.Now, false);//

6. Weeks of the year

private int WeekOfYear() { var dt = DateTime.Now; int firstWeekend = Convert.ToInt32(DateTime.Parse(dt.Year + "-1-1").DayOfWeek); int weekDay = firstWeekend == 0 ? 1 : (7 - firstWeekend + 1); int currentDay = dt.DayOfYear; int current_week = Convert.ToInt32(Math.Ceiling((currentDay - weekDay) / 7.0)) + 1; return current_week; }

7. Mondays and Sundays in previous weeks

private void FEDayInLastWeek() { int N = 3;//parameters of previous weeks DateTime Today = DateTime.Now; int daysInWeek = (int)Today.DayOfWeek == 0 ? 7 : (int)Today.DayOfWeek;//the day of the current week, note: Sunday is 0 for (int i = N; i > 0; i--) { //Start Date DateTime firstDay = Today.AddDays(1 - (7 * i + daysInWeek)); DateTime lastDay = Today.AddDays(7 - (7 * i + daysInWeek)); } }

8. This Monday and the Current Day

private void FristDayToNowInThisWeek() { int daysInWeek = (int)DateTime.Now.DayOfWeek == 0 ? 7 : (int)DateTime.Now.DayOfWeek;//the day of the week, note: Sunday is 0 //Start Date DateTime firstDay = DateTime.Now.AddDays(1 - daysInWeek); DateTime lastDay = DateTime.Now; }

C#controls bind time field formatting

Bind("queried fields", "{0: yy/MM/dd}") At this point, I believe everyone has a deeper understanding of "what are the common date and time methods of C#". You may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Date time method Craft commonly used Timeless comment content Tianhe English Learning query start practical Deep interest Parameter practical actual year Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Xiaomi NVidia Shulou Tech Info