# c gets sample code for the number of weeks in the current year and the number of days in the current month

• 2020-06-03 08:06:39
• OfStack

Gets the number of weeks in the current year

``````
/// <summary>
///  Get a few weeks of this year
/// </summary>
/// <param name="year"></param>
/// <returns></returns>
public int GetWeekOfYear(int year)
{
DateTime the_Date = new DateTime(year,1,1);// This year the first 1 day
TimeSpan tt=the_Date.AddYears(1)-the_Date;// Figure out how many days are in the current year
return tt.Days / 7 + 1; // Because there are only 366 Days and 365 Day divided by the 7 It has a remainder so it always has to be added 1 weeks
}
``````

Gets the number of days of the current month

1 species:

``````
DateTime dt = new DateTime(2007, 1, 1);
// in .net Internal processing is subtracting +1 The next month gets the number of days of the current month ( And the way to do that is actually to figure out the difference ) For example, :
// Is the current month 2007 years 1 month 1 day , through AddMonths Method to add 1 Months is 2007 years 2 month 1 day , in .net We're going to figure out how many days apart they are .
// So we can use this method to figure out how many days are in the current month
TimeSpan tt = dt.AddMonths(1) - dt;
// DateTime dt = new DateTime(2007,1,31);  If the time is 2007,1,31 add 1 A: Not every month minus the current month , Here's the explanation :
/* Like for you to do 1 A project, 2010-01-01 Made for you, 1 In a month, yes 2010-02-01 delivery
now 2010-01-31 Number for you to do, is also a requirement 1 Finished in two months, was supposed to be in 2010-02-31 Delivered by No., but 2 On no 31 So the time of delivery becomes 2010-02-28 The no.
Response.Write(tt);
``````

The second:
``````
public static int year;
public static int month;
public static int days;
protected void Button1_Click(object sender, EventArgs e)
{
year = DateTime.Now.Year;
month = DateTime.Now.Month;
// Gets the number of days of the current month
switch (month)
{
case 1:
days = 31;
break;
case 2:
if (DateTime.IsLeapYear(year))
{
// A leap year 2 Month for 29 day
days = 29;
}
else
{
// Not leap years, 2 Month for 28 day
days = 28;
}
break;
case 3:
days = 31;
break;
case 4:
days = 30;
break;
case 5:
days = 31;
break;
case 6:
days = 30;
break;
case 7:
days = 31;
break;
case 8:
days = 31;
break;
case 9:
days = 30;
break;
case 10:
days = 31;
break;
case 11:
days = 30;
break;
case 12:
days = 31;
break;
}
TextBox1.Text = days.ToString();
}
``````

``````
protected void Page_Load(object sender, EventArgs e)
{
int day=GetWeekDay(2010,11);
if (day != 0)
{
Response.Write(day);
}
}
public int GetWeekDay(int year,int month)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;

case 2:
if (DateTime.IsLeapYear(year))
return 29;
else
return 28;

case 4:
case 6:
case 9:
case 11:
return 30;
default:
return 0;
}
``````

Related articles: