recently has a project to use the annual cycle for the use of data statistics diagrams, using the annual cycle, and the date range in which the annual cycle is located. At first, it was assumed that the year cycle was converted by known data, and it was more complicated after searching data was found through database SQL statement. Now, change the thinking through the C# background code to calculate the two period range of the date range, in accordance with the year cycle corresponding to the date range of the database query for statistics. We need to solve the following two point problems,

first: find the week of the year in which the date is based on the date;

second: calculate the date range of the period according to the cycle of the year.

 using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; AR (); int weekOfYear = gc.GetWeekOfYear (DateTime.Now, CalendarWeekRule.FirstDay, DayOfWeek.Monday); Console.WriteLine ("current week {0}", weekOfYear); DateTime startDate. Date, out lastDate); Console.WriteLine ("week {0}", I); Console.WriteLine (startDate); Console.WriteLine (lastDate);} Console.ReadLine (); {} {} TeTime.MinValue; last = DateTime.MinValue; if (year < 1700, year > 9999) {/ / / / / "year limit" return false;} if (1) {/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / Year, 1, 1); / / / / / / / the first day of the year DateTime endDay = new DateTime (year + 1, 1, 1).AddMilliseconds (-1); int dayOfWeek = 0; if (Convert.ToInt32); 0); / / / / / the first day of the year If (dayOfWeek = = = 0) {dayOfWeek = 7;} if (index = = 1) {first = startDay.AddDays (7 - dayOfWeek - 6); if (dayOfWeek = = 6) {last = first;} {{} = {};}} Else {first = startDay.AddDays ((8 - dayOfWeek) + (index - 2) * 7); the starting date of the //index week last = first.AddDays (6); //if (last > endDay) / / / /} {/ / / / "the number of input weeks is greater than the maximum number of weeks of the year"; return false;} return true;}}

execution result

summary of

above is a small compilation of C# to get the current year of the cycle and the date range of the date range (recommended), I hope to help you, if you have any questions welcome to give me a message, the small editor will respond to you in time!


This concludes the body part

This paper fixed link:http://www.script-home.com/c-gets-the-cycle-and-date-range-of-the-current-year-recommended.html | Script Home | +Copy Link

Article reprint please specify:C# gets the cycle and date range of the current year (recommended) | Script Home

You may also be interested in these articles!