DateTime简单使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Ex7._18
{
    class Program
    {
        
        static void Main(string[] args)
        {   
           
        //A:
           //DateTime类介绍
            //定义时间类
            Console.WriteLine("定义时间类 ");
            DateTime dt1, dt2, dt3;
            string str1, str2,str3;
            dt1 = new DateTime(2013, 7, 18,15,1,1);
            Console.WriteLine(dt1 +"\n\n");

            //获取当前时间
            Console.WriteLine("获取当前时间 ");
            dt2 = DateTime.Now;
            Console.WriteLine(dt2);
            int y, m, d,h,min,sec;

            //获取年月日 时分秒
            Console.WriteLine("获取年月日 时分秒");
            y = dt2.Year;
            m = dt2.Month;
            d = dt2.Day;
            h = dt2.Hour;
            min = dt2.Minute;
            sec = dt2.Second;
            Console.WriteLine("{0} {1} {2} {3} {4} {5} \n\n",y,m,d,h,min,sec);

            //输出的时间类型
            Console.WriteLine("输出的时间类型");
            str1 = dt2.ToLongDateString();
            Console.WriteLine(str1);
            str2 = dt2.ToShortDateString();
            Console.WriteLine(str2);

            //自定义Format输出格式
            Console.WriteLine("自定义Format输出格式");
            str3 = string.Format("{0:yyyy-MM-dd hh:mm:ss}", dt1);
            Console.WriteLine(str3);
            str3 = string.Format("{0:yyyy  MM dd HH:mm:ss}", dt1);
            str3 = string.Format("{0:yyyy-MM-dd hh:mm:ss}", dt1);
            Console.WriteLine(str3+"\n\n");

           //时间加减
            Console.WriteLine("时间加减");
            dt2 = dt2.AddDays(10);
            Console.WriteLine(dt2);
            dt2 = dt2.AddDays(-100);
            Console.WriteLine(dt2);
            dt2 = dt2.AddYears(100);
            Console.WriteLine(dt2+"\n\n");

            //TimeSpan类来计算时间差
            Console.WriteLine("TimeSpan类来计算时间差");
            DateTime d1 = new DateTime(2004, 1, 1, 15, 36, 05);
            DateTime d2 = new DateTime(2005, 3, 1, 20, 16, 35);
            TimeSpan cha1 = d2.Subtract(d1);
            double totalday, totalhour;
            totalday = cha1.TotalDays;
            totalhour = cha1.TotalHours;
            Console.WriteLine("相差总共{0}天 或者总计相差{1}小时", totalday, totalhour);
            Console.WriteLine("相差{0}天 {1}小时 {2}分 {3}秒\n\n", cha1.Days, cha1.Hours, cha1.Minutes, cha1.Seconds);
          //  goto A;

        }
    }
}




                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值