//本计算机的当前日期和时间
DateTime dt = DateTime.Now;
//默认用本地的日期格式设置来输出
Console.WriteLine("当前时间 = " + dt);
// HH 是24制的小时,hh 12制的小时
Console.WriteLine("当前时间 = " + dt.ToString("yyyy年MM月dd日 HH时mm分ss秒"));
Console.WriteLine("当前时间 = " + dt.ToString("yy年M月d日 HH时mm分ss秒"));
Console.WriteLine("当前时间 = " + dt.ToString("yy年M月d日 HH时mm分ss秒"));
for (int i = 0; i < 100; i++)
{
dt = DateTime.Now;
int year = dt.Year;
int month = dt.Month;
int day = dt.Day;
int hour = dt.Hour;
int m = dt.Minute;
int s = dt.Second;
int ms = dt.Millisecond;
Console.WriteLine("年 = " + year + " 月 = " + month
+ " 日 = " + day + " 小时 = " + hour +
" 分钟 = " + m + " 秒 = " + s +
" 毫秒 = " + ms);
// 线程休息
//Thread.Sleep(1000);
}
Console.WriteLine("日期加正数 = " + dt.AddDays(30));
Console.WriteLine("日期加负数 = " + dt.AddDays(-7));
Console.WriteLine("当前时间 = " + dt);
//表示时间间隔
TimeSpan time = new TimeSpan(-10, -2, 0, 0, 0);
Console.WriteLine("当前时间加时间间隔 = " + dt.Add(time));
DateTime newDt = dt.AddDays(2);
// 计算时间差
TimeSpan ts = newDt - dt;
// 总的小时数
Console.WriteLine("新时间 = " + newDt + " 时间差 = " + ts.TotalSeconds);
try
{
//字符串转时间
DateTime nDT = DateTime.Parse("2015年8月13日 14:09:12");
//通用转换器
Convert.ToDateTime("2015年8月13日 14:09:12");
}
catch
{
}
DayOfWeek dow = dt.DayOfWeek;//枚举
Console.WriteLine("星期几 = " + (int)dt.AddDays(2).DayOfWeek);
Console.WriteLine("今天是今年的第几天 " + dt.DayOfYear);
dt = DateTime.Today;//当前的时间
Console.WriteLine("当前日期 = " + dt);
Console.ReadLine();
double pai = Math.PI;
Console.WriteLine("圆周率="+pai);
double a = Math.Ceiling(145.6);
Console.WriteLine("上限数值="+a );
double b = Math.Floor(145.6);
Console.WriteLine("下线数值=" + b);
double c = Math.Round(123.5);
Console.WriteLine("四舍五入的值="+c );
double d = Math.Sqrt(4);
Console.WriteLine("平方根的值为+"+d );
double e = Math.Pow(8,2);
Console.WriteLine("指定次幂="+e );
Console.ReadLine();