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;
}
}
}
DateTime简单使用
最新推荐文章于 2023-08-16 14:57:50 发布