自定义一个日期类,该类包含年,月,日字段与属性,具体有将日期增加一天,一个月,一年的方法,具有单独显示年,单独显示月,单独显示日的方法和年月日一直显示的方法
- 提问者网友:蓝琪梦莎
- 2021-04-23 06:19
- 五星知识达人网友:平生事
- 2021-04-23 06:59
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DefDateTime d = new DefDateTime(2009, 10, 18);
DateTime dt = d.addmonth();
Console.WriteLine(dt);
Console.ReadKey();
}
static int ResultSum(params int[] x)
{
int sum = 0;
foreach (int i in x)
sum += i;
return sum;
}
public class DefDateTime
{
public int year;
public int month;
public int day;
public int Year
{
get { return year; }
set { year = value; }
}
public int Month
{
get { return month; }
set { month = value; }
}
public int Day
{
get { return day; }
set { day = value; }
}
public DateTime addDay()
{
DateTime dt = DateTime.Parse(year.ToString() + "-" + month.ToString() + "-" + day.ToString());
return dt.AddDays(1);
}
public DateTime addmonth()
{
DateTime dt = DateTime.Parse(year.ToString() + "-" + month.ToString() + "-" + day.ToString());
return dt.AddMonths(1);
}
public DateTime addyear()
{
DateTime dt = DateTime.Parse(year.ToString() + "-" + month.ToString() + "-" + day.ToString());
return dt.AddYears(1);
}
public DefDateTime(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
}
}
}