When we do some business systems, we often use some time-of-access scenarios. For example, you need to count some business data for a certain period of time, such as a week, a month, or a year. At this point we need to get some start and end dates in the current time period. Here is a generic date helper class that allows you to quickly get the starting and ending dates of week, Month, and year based on the date passed in.
public static Class Datehelper
{
public static DateTime Weekfirstday (this datetime dt)
{
DT = datetime.parse (dt. ToString ("Yyyy-mm-dd"));
if (dt. DayOfWeek = = DayOfWeek.Sunday)
{
Return dt. AddDays (-6);
}
Return dt. AddDays (1-(int) dt. DayOfWeek);
}
public static DateTime Weekendday (this datetime dt)
{
DT = datetime.parse (dt. ToString ("Yyyy-mm-dd"));
if (dt. DayOfWeek = = DayOfWeek.Sunday)
{
return DT;
}
Return dt. AddDays (7-(int) dt. DayOfWeek);
}
public static DateTime Monthfirstday (this datetime dt)
{
DT = datetime.parse (dt. ToString ("Yyyy-mm-dd"));
Return dt. AddDays (1-DT. Day);
}
public static DateTime Monthendday (this datetime dt)
{
DT = datetime.parse (dt. ToString ("Yyyy-mm-dd"));
Return dt. AddDays (1-DT. Day). AddMonths (1). AddDays (-1);
}
public static DateTime Yearfirstday (this datetime dt)
{
return new DateTime (dt. Year, 1, 1);
}
public static DateTime Yearendday (this datetime dt)
{
return new DateTime (dt. Year, 12, 31);
}
}
Week,month, Year date interval auxiliary class