life's a journey not a destination

Tính toán chính xác đến giây, milli giây và micro giây dùng C#

Leave a comment

Dùng DateTime.Ticks. Một tick đại diện 1 trăm nano giây.

using System;

namespace ConsoleApplication2
    class Program
        static void Main(string[] args)
                long startTick = DateTime.Now.Ticks;
                long endTick = DateTime.Now.Ticks;
                long tick = endTick - startTick;
                long seconds = tick / TimeSpan.TicksPerSecond;
                long milliseconds = tick / TimeSpan.TicksPerMillisecond;
                long microseconds = tick / 10;
                Console.WriteLine("Seconds Elapsed :" + seconds);
                Console.WriteLine("Millseconds Elapsed :" + milliseconds);
                Console.WriteLine("Microseconds Elapsed :" + microseconds);
            catch (Exception ex)
                // handle ex


Author: dinhnn

Senior software developer, a technical leader. You can be reached at via email to dinhnguyenngoc@gmail.com, via my blog at dinhnguyenngoc.wordpress.com, and on Twitter @dinhnguyenngoc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s