namespace DayTime类和TimeSpan类
{
class MainClass
{
public static void Main(string[] args)
{
//使用DataTime类创建一个DateTime对象dt,并赋值2018-7-23
DateTime dt = new DateTime(2018, 7, 25);
//将对象dt以断日期格式你显示出来
Console.WriteLine(dt.ToShortDateString());
Console.WriteLine("2018年7月13日是本年度的第{0}天",dt.DayOfYear);
//输出对象dt的月份值
Console.WriteLine("月份:{0}",dt.Month.ToString());
Console.WriteLine("月份:{0}", dt.Month);//ToString为什么可省?下同
//使用TimeSpan类创建一个TimeSpan对象ts,并赋值
TimeSpan ts = dt - DateTime.Now;//Datetime.Now表示当前日期
TimeSpan ts1 = DateTime.Now- dt;// ’-‘为减法符
Console.WriteLine("距离2018年7月25日还有{0}天",ts.Days.ToString());
//输出与上一个为相反数
Console.WriteLine("距离2018年7月25日还有{0}天", ts1.Days.ToString());
}
}
}