How to get the last day of a month

Source: Internet
Author: User

Often we encounter such a problem: first, the last day of a month is 29, 30, or 31? The second is to return to the last day of a month. This problem if, inadvertently, the SQL of the date of the statement, contains the wrong date, then there will be errors, then how to solve it? Please refer to the following code.

public string Monthmaxday (long Year,long month,int Flag)
{//Get month  Livestock Day
String value,svalue = "";D atetime Dt;int Day;
if (month==0)
{
month=month+1;
Year=year-1;
}
if (flag>0)
{
svalue=year.tostring ("0000") +month.tostring ("00") + "28";
for (day=29;day<=31;day++)
{
Try
{
Value= month.tostring ("") + "/" + Day + "/" +year.tostring ("0000");
Dt=convert.todatetime (Value);
value=year.tostring ("0000") +month.tostring ("a") +day;
Svalue=value;
}
catch (Exception Errdis)
{
Errdiscroption=errdis.tostring ();
Goto ERRHD;
}
}
}
Else
{
svalue=year.tostring ("0000") +month.tostring ("00") + "01";
}
ERRHD:
return svalue;
}



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.