using System;

namespace DayTime类和TimeSpan类
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            //使用DataTime类创建一个DateTime对象dt,并赋值2018-7-23
            DateTime dt = new DateTime(2018725);
            //将对象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());
        }
    }

}

(c#第三天)DayTime类和TimeSpan类实例

相关文章:

  • 2021-05-20
  • 2022-12-23
  • 2022-01-27
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-17
  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-12
  • 2022-12-23
相关资源
相似解决方案