Calculate the start and end dates of each year, month, and week.

Source: Internet
Author: User

When writing a program, we usually calculate the start date and end date of year, month, and week. Here we provide a unified solution.

/// <Summary>
/// Type Enumeration During Declaration
/// </Summary>
Public Enum period {day, week, month, year };
/// <Summary>
/// Obtain the start and end dates of the specified period
/// </Summary>
/// <Param name = "Period"> Period type </param>
/// <Param name = "begindate"> Start date </param>
/// <Param name = "enddate"> end date </param>
Public static void getperiod (period, out datetime begindate, out datetime enddate)
{
Int year = datetime. Today. Year;
Int month = datetime. Today. month;
Switch (period)
{
Case period. Year: // year
Begindate = new datetime (year, 1, 1 );
Enddate = new datetime (year, 12, 31 );
Break;
Case period. Month: // month
Begindate = new datetime (year, month, 1 );
Enddate = begindate. addmonths (1). adddays (-1 );
Break;
Case period. Week: // week
Int week = (INT) datetime. Today. dayofweek;
If (week = 0) Week = 7; // Sunday
Begindate = datetime. Today. adddays (-(week-1 ));
Enddate = begindate. adddays (6 );
Break;
Default: // day
Begindate = datetime. today;
Enddate = datetime. today;
Break;
}
}

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.