Week,month, Year date interval auxiliary class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.