Asp.net obtains the first and last day examples of the current month.

Source: Internet
Author: User

The code for getting the first day and last day of the month is as follows:

The code is as follows: Copy code

DateTime now = DateTime. Now;
DateTime dt1 = new DateTime (now. Year, now. Month, 1 );

DateTime dt2 = d1.AddMonths (1). AddDays (-1 );

Dt1 is the first day of this month, and dt2 is the last day of this month,
The algorithm for the last day is: get the first day of the month, then add January, and then subtract one day.

Example

The code is as follows: Copy code

DateTime dt = DateTime. Now;
// The first day of the month
DateTime dt_First = dt. AddDays (-(dt. Day) + 1 );
Label1.Text = dt_First.ToString ("yyyy-MM-dd ");
// Add the number of months in the current month to 1
DateTime dt2 = dt. AddMonths (1 );
// Last day of the month
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;

We all need to write it by hand. The following code can be used for intelligent judgment.

In this example, you can intelligently determine the number of days each month. You do not need to calculate the number of days. This function will be completed for you:

 

The code is as follows: Copy code
Using System;
Namespace DotNet. Utilities
{
Public class DateFormat
    {
// Return the first and last days of each month.
Public static void ReturnDateFormat (int month, out string firstDay, out string lastDay)
        {
Int year = DateTime. Now. Year + month/12;
If (month! = 12)
            {
Month = month % 12;
            }
Switch (month)
            {
Case 1:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 ");
Break;
Case 2:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
If (DateTime. IsLeapYear (DateTime. Now. Year ))
LastDay = DateTime. Now. ToString (year + "-0" + month + "-29 ");
Else
LastDay = DateTime. Now. ToString (year + "-0" + month + "-28 ");
Break;
Case 3:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString ("yyyy-0" + month + "-31 ");
Break;
Case 4:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-0" + month + "-30 ");
Break;
Case 5:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 ");
Break;
Case 6:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-0" + month + "-30 ");
Break;
Case 7:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 ");
Break;
Case 8:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-0" + month + "-31 ");
Break;
Case 9:
FirstDay = DateTime. Now. ToString (year + "-0" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-0" + month + "-30 ");
Break;
Case 10:
FirstDay = DateTime. Now. ToString (year + "-" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-" + month + "-31 ");
Break;
Case 11:
FirstDay = DateTime. Now. ToString (year + "-" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-" + month + "-30 ");
Break;
Default:
FirstDay = DateTime. Now. ToString (year + "-" + month + "-01 ");
LastDay = DateTime. Now. ToString (year + "-" + month + "-31 ");
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.