Roger Luo

超越梦想一起飞
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C# 时间操作

Posted on 2013-01-24 21:43  Roger Luo  阅读(145)  评论(0编辑  收藏  举报

1. Show current time

 

2. Calculate execution time

Use StopWatch classs, which locates in System.Diagnostics namespace.

static void Main(string[] args)
{
Stopwatch s1 = new Stopwatch();
s1.Start();
Thread.Sleep(891);
s1.Stop();
Display(s1);

s1.Restart();
Thread.Sleep(100);
s1.Stop();
Display(s1);

if (Stopwatch.IsHighResolution)
Console.WriteLine("High Resolution");
else
Console.WriteLine("Not High Resolution");

Console.WriteLine(Stopwatch.Frequency);
}

static void Display(Stopwatch s1)
{
Console.WriteLine(s1.ElapsedMilliseconds);
Console.WriteLine(s1.ElapsedTicks);
TimeSpan sp1 = s1.Elapsed;
Console.WriteLine(string.Format("{0:00}:{1:00}:{2:00}.{3:00}", sp1.Hours,
sp1.Minutes,
sp1.Seconds,
sp1.Milliseconds / 10));
}