
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleTest
{
class Program
{
static void Main(string[] args)
{
int y = 2006, m = 12, d = 32;
Console.WriteLine("{0}-{1}-{2}", y, m, d);
fun(ref y, ref m, ref d);
Console.WriteLine("{0}-{1}-{2}", y, m, d);
Console.ReadKey();
}
//使用一个方法来变换年月日
private static void fun(ref int year,ref int month,ref int day)
{
int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //每月的天数
if(year%400==0||(year%4==0&&year%100!=0)) //闰年2月29天
{
days[1] = 29;
}
if (day <= days[month - 1]) return; //天数不超过当月的天数,直接返回
if(month==12) //如果是12月,则要转到明年的1月
{
year += 1;
month = 1;
day -= 31;
}
else //一般的情况
{
day -= days[month - 1];
month += 1;
}
}
}
}