C# 时间操作

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));
}

原文地址:https://www.cnblogs.com/rogerroddick/p/2875676.html