Add or subtract a date using a self-written Algorithm

Source: Internet
Author: User
Private string jia7 (datatable DT, int dd) // calculate the correct date after 7 days
{
String date = "";
Int riqi = 0, days = 0, Day = 0;
String date = DT. Rows [0] [0]. tostring ();
Int testdate = convert. toint32 (date. substring (); // year
Int testdate1 = convert. toint32 (date. substring (); // month
Int testdate2 = convert. toint32 (date. substring (6, 2); // obtain the date
Days = Yuefen (testdate1); // verify the month and give the correct number of days
Riqi = convert. toint32 (testdate2) + dd;
If (riqi> days) // if the date after 7 days is greater than the date of this month
{
If (testdate1 = 12) // if the current month is January 1, December
{
Day = Yuefen (1); // upload the day January back to calculate the day January
Testdate1 = 1; // change the month to February.
Testdate = testdate + 1; // and Add 1 to the current year
}
Else
{
Testdate1 = testdate1 + 1; // Add 1 to the current month
Day = Yuefen (testdate1 );
}
Day = riqi-days; // subtract the date of the new month from the date of the last seven days.
Date + = testdate; // Add the correct year
If (testdate1> 0 & testdate1 <10)
{
Date + = 0;
Date + = testdate1;
}
Else
{
Date + = testdate1; // Add the correct month
}
If (day> 0 & day <10)
{
Date + = 0;
Date + = Day;
}
Else
{
Date + = day; // Add the correct date
}
}
Else
{
Date + = testdate;
If (testdate1> 0 & testdate1 <10)
{
Date + = 0;
Date + = testdate1;
}
Else
{
Date + = testdate1; // Add the correct month
}
If (riqi> 0 & riqi <10)
{
Date + = 0;
Date + = riqi;
}
Else
{
Date + = riqi; // Add the correct date
}
}
Return date;
}
Private string jian7 (datatable DT, int dd) // calculate the correct date after 7 days
{
String date = "";
Int riqi = 0, days = 0, Day = 0;
String date = DT. Rows [0] [0]. tostring ();
Int testdate = convert. toint32 (date. substring (); // year
Int testdate1 = convert. toint32 (date. substring (); // month
Int testdate2 = convert. toint32 (date. substring (6, 2); // obtain the date
Days = Yuefen (testdate1); // verify the month and give the correct number of days
Riqi = convert. toint32 (testdate2)-dd; // subtract 7 days from the number of days obtained
If (riqi <1) // if the result is smaller than 1, reduce it by one month.
{
If (testdate1 = 1) // if the current month is January 1, January
{
Day = Yuefen (12); // The number of days passed for December 1, December
Testdate1 = 12; // change the month to January 1, December.
Testdate = testDate-1; // The Year of month rollback is also rolled back
}
Else
{
Testdate1 = testDate1-1; // subtract 1 from the retrieved month
Day = Yuefen (testdate1 );
}
Day = day + riqi; // calculate the correct date
Date + = testdate; // Add the correct year
If (testdate1> 0 & testdate1 <10)
{
Date + = 0;
Date + = testdate1;
}
Else
{
Date + = testdate1; // Add the correct month
}
If (day> 0 & day <10)
{
Date + = 0;
Date + = Day;
}
Else
{
Date + = day; // Add the correct date
}
}
Else
{
Date + = testdate;
If (testdate1> 0 & testdate1 <10)
{
Date + = 0;
Date + = testdate1;
}
Else
{
Date + = testdate1; // Add the correct month
}
If (riqi> 0 & riqi <10)
{
Date + = 0;
Date + = riqi;
}
Else
{
Date + = riqi; // Add the correct date
}
}
Return date;
}
Private int Yuefen (INT testdate1) // accept the month to calculate the correct number of days of the month
{
Int days = 0;
Switch (testdate1) // verify the month and give the correct number of days
{
Case 1:
Days = 31;
Break;
Case 2:
If (convert. toint32 (testdate1) % 4 = 0)
{
Days = 29;
}
Else
{
Days = 28;
}
Break;
Case 3:
Days = 31;
Break;
Case 4:
Days = 30;
Break;
Case 5:
Days = 31;
Break;
Case 6:
Days = 30;
Break;
Case 7:
Days = 31;
Break;
Case 8:
Days = 31;
Break;
Case 9:
Days = 30;
Break;
Case 0:
Days = 31;
Break;
Case 11:
Days = 30;
Break;
Case 12:
Days = 31;
Break;
}
Return days;
}

 

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.