Search code examples
.netwindowsutcsystemtime

DateTime.UtcNow Not Change, Why?


check the code

        StringBuilder s = new StringBuilder();
        for (var i = 0; i < 100; i++)
        {
            TimeSpan t = (DateTime.UtcNow - new DateTime(2010, 1, 1));
            ulong timestamp = (ulong)(t.TotalMilliseconds * 100000000);
            s.Append("<li>" + timestamp.ToString());
        }

You will get same result 100 times, it means the DateTime.UtcNow never changes even I multiply milliseconds by 100000000 times.

Is here anyone know how to get fresh DateTime.UtcNow each time?


Solution

  • In such a tight loop chances are indeed good that you will be getting the same time.

    Each tick in the Ticks property represent a 100 nano-second slice. Your code would iterate faster than that.