Look at the docs for DateTime.Millisecond says the value can be 0-999 which makes complete sense.
However looking at the docs for DateTime.Nanosecond says the value can be 0-900 and for DateTime.Microsecond it can be 0-999
I don't get the last two unless it's nanoseconds to microseconds then microseconds to milliseconds?
Can someone explain it better?
Look at the docs for DateTime.Millisecond says the value can be 0-999 which makes complete sense.
However looking at the docs for DateTime.Nanosecond says the value can be 0-900 and for DateTime.Microsecond it can be 0-999
I don't get the last two unless it's nanoseconds to microseconds then microseconds to milliseconds?
Can someone explain it better?
Share edited Mar 21 at 18:57 M-- 29.5k10 gold badges70 silver badges106 bronze badges asked Mar 9 at 0:11 user3161924user3161924 2,3911 gold badge23 silver badges43 bronze badges 7 | Show 2 more comments3 Answers
Reset to default 4The Nanosecond
property is effectively "nanosecond-within-a-microsecond", just as the Microsecond
property is "microsecond-within-a-millisecond".
So if you have a value of 2025-03-12T00:00:00.123456700, then you'd have properties of:
Millisecond
: 123Microsecond
: 456Nanosecond
: 700
The precision of DateTime
is only "tick" (100ns) which is why Nanosecond
is always a multiple of 100.
Personally I'd have named this NanosecondOfMicrosecond
to avoid ambiguity, just as (in my Noda Time project) I've got NanosecondOfSecond
(and no-one has ever requested NanosecondOfMicrosecond
, interestingly).
According to the .NET 9 API reference, System.DateTime
time values are measured in 100-nanosecond units called ticks (https://learn.microsoft/en-us/dotnet/fundamentals/runtime-libraries/system-datetime). Basically, DateTimes are inherently constrained in their functionality due to the implementation, which prohibits you from using lower values.
Here is a representation of the International System of Units with the addition of tick values:
1 tick = 100 ns
1 μs = 1000 ns (equivalent to 10 ticks)
1 ms = 1000 μs (or 1,000,000 ns or 10,000 ticks)
Whereas ns stands for nanoseconds, ms stands for milliseconds, and μs stands for microseconds.
The precision of System.DateTime
is ticks, internally, the value of System.DateTime
is a 64-bit Integer of the number of ticks since since 12:00:00 midnight, January 1, 0001.
DateTime values and their string representations Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight, January 1, 0001. The actual DateTime value is independent of the way in which that value appears when displayed. The appearance of a DateTime value is the result of a formatting operation that converts a value to its string representation.
Nanoseconds has been available since .Net 7 and are calculated by multiplying the raw tick count by 100. This is offered so thatyou don't have to do that conversion yourself when you need it. This was introduced with changes to TimeSpan
that also added support for Nanoseconds. The internal implementation means that if you were to try to set the nanoseconds either from parsing or serialization then an Overflow exception would occur if the value was outside of the supported range:
Unhandled exception. System.OverflowException: The TimeSpan string '10:20:30.123456700' could not be parsed because at least one of the numeric components is out of range or contains too many digits.
- If you need to record durations with nanosecond precision, it is generally recommended that you store this value in an integer form instead of using
System.DateTime
Now what will really bake your noodle is how can System.TimeSpan.TotalNanoseconds
return fractional nanoseconds?
Gets the value of the current TimeSpan structure expressed in whole and fractional nanoseconds.
double
is used to remain consistent with the other total properties, but because the total number of ticks is 18 digits (for today's date) and the real total number of Nanoseconds is 20 digits, this is well outside the precision of double
that is stored in 8 bytes
Characteristics of the floating-point types
C# type/keyword Approximate range Precision Size .NET type float ±1.5 x 10−45 to ±3.4 x 1038 ~6-9 digits 4 bytes System.Single double ±5.0 × 10−324 to ±1.7 × 10308 ~15-17 digits 8 bytes System.Double decimal ±1.0 x 10-28 to ±7.9228 x 1028 28-29 digits 16 bytes > System.Decimal
If you need to calculate the TotalNanoseconds with accuracy, then the best you can do is calculate against the ticks and multiply the result by TimeSpan.NanosecondsPerTick
(100)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744882130a4598900.html
NanosecondOfSecond
. (We don't haveNanosecondOfMicrosecond
at all, but that's what we would call whatDateTime.Nanosecond
expresses, if we had it...) – Jon Skeet Commented Mar 9 at 6:46