asp.Net to get the first day of the week the first day of the month and so on

  • 2020-05-07 19:27:16
  • OfStack


DateTime dt = DateTime.Now; 
// The first of this month 1 Day time  
DateTime dt_First = dt.AddDays(-(dt.Day) + 1); 
Label1.Text = dt_First.ToString("yyyy-MM-dd"); 
// Count the months +1 
DateTime dt2 = dt.AddMonths(1); 
// The last of this month 1 Day time  
DateTime dt_Last = dt2.AddDays(-(dt.Day)); 
Label2.Text = dt_Last.ToString("yyyy-MM-dd"); 
int weeknow = Convert.ToInt32(System.DateTime.Now.DayOfWeek); 
int daydiff = (-1) * weeknow + 1; 
int dayadd = 7 - weeknow; 
 // This week the first 1 day  
lblBegin.Text = System.DateTime.Now.AddDays(daydiff).ToString("yyyy-MM-dd"); 
// This week the last 1 day  
lblEnd.Text = System.DateTime.Now.AddDays(dayadd).ToString("yyyy-MM-dd"); 

Related articles: