Reputation: 91
I have been using C# for quite some time, and have suddenly come across DateTime.Now.Ticks
.
What is it for?
Upvotes: 7
Views: 14582
Reputation: 1501043
It represents the total number of ticks in local time (not UTC) since the DateTime epoch, which is midnight on January 1st in the year 1AD. (Each tick is 100 nanoseconds; there are 10,000 ticks in a millisecond.)
To break it down, DateTime.Now
is a static property returning a DateTime
representing the current time.
Then DateTime.Ticks
is an instance property on DateTime, returning the number of ticks since midnight on January 1st, 1AD. It gets more complicated due to time zones (and DateTime's poor design) but that's the basics.
A tick is the smallest unit of time used in DateTime
and TimeSpan
. You typically use it to make sure you can completely round-trip a value without losing any information.
Note that it's not the same thing as the ticks returned by Stopwatch.ElapsedTicks
, which are system-dependent; their length can be determined using StopWatch.Frequency
.
Upvotes: 13
Reputation: 1
I'm guessing it's the safest way to communicate time between disparate systems between potentially different technology stacks.
a common guaranteed way to communicate time without worrying about structure, format and definitions of date time strings, leaving it up to the implementing developer to read and interpret
Upvotes: -2