The last day of the month is the first week
private int Getweeknuminmonth (DateTime daytime)
{
int dayinmonth = daytime. Day;
First day of the month
DateTime FirstDay = daytime. AddDays (1-daytime. Day);
The first day of the month is a few weeks
int weekday = (int) Firstday.dayofweek = = 0? 7: (int) Firstday.dayofweek;
A few days in the first week of this month
int firstweekendday = 7-(weekday-1);
Difference between the current date and the first week
int diffday = Dayinmonth-firstweekendday;
Diffday = diffday > 0? Diffday:1;
It's the first week, minus 7 a day if it's divisible.
int weeknuminmonth = ((diffday% 7) = = 0
? (DIFFDAY/7-1)
: (DIFFDAY/7)) + 1 + (Dayinmonth > Firstweekendday? 1:0);
return weeknuminmonth;
}
<summary>
Start date of week week before calculation
</summary>
<param name= "Week" > needed to calculate the previous week week (0 for the previous week, 1 for the previous week ...). ) </param>
<param name= "Dt_starttime" > Week 1st </param>
<param name= "Dt_starttime" > Sunday Day </param>
<returns></returns>
public void Getweekstartenddate (DateTime DT, Int. week, out datetime dt_starttime, out datetime dt_endtime)
{
int n_dayofweek = Int. Parse (dt. Dayofweek.tostring ("D"));
Dt_starttime = datetime.parse (dt. AddDays (-n_dayofweek-week * 7 + 1). ToString ("Yyyy/mm/dd"));
Dt_endtime = datetime.parse (dt. AddDays (-n_dayofweek-(week-1) * 7). ToString ("Yyyy/mm/dd"));
if (dt_endtime > dt)
{
dt_endtime = DT;
}
}
The calculation time is applied to the week of the year
public int getweekofyear (DateTime dt)
{
int firstweekend = 7-convert.toint32 (datetime.parse (dt. Year + "-1-1"). DayOfWeek);
int currentday = dt. DayOfYear;
Return Convert.ToInt32 (Math.ceiling ((currentday-firstweekend)/7.0) + 1;
}
Calculate the starting date for this week
public datetime getcurrentweekstartdate (DateTime dt)
{
int n_dayofweek = Int. Parse (dt. Dayofweek.tostring ("D"));
Return DateTime.Parse (dt. AddDays (-n_dayofweek + 1). ToString ("Yyyy/mm/dd"));
}
Asp. NET calculation date is the first week of this month