Understanding C# DateTime MicroSecondNanoSeconds? - Stack Overflow

Look at the docs for DateTime.Millisecond says the value can be 0-999 which makes complete sense.Howev

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
  • 2 The nanoseconds component, expressed as a value between 0 and 900 (in increments of 100 nanoseconds). Keyword being component. It's the part of DateTime that is the remainder and cannot fill the bigger component. 59 seconds - not a minute yet. – Ivan Petrov Commented Mar 9 at 0:31
  • 4 The page you linked to says "in increments of 100 nanoseconds". I feel that's a good enough explanation on its own. – gunr2171 Commented Mar 9 at 1:15
  • 1 @gunr2171 why wouldn't it be 0-9,999,999 ? and how is 0-999 a microsecond? – user3161924 Commented Mar 9 at 1:24
  • @user3161924 For the same reason you don't have 9,999,999 seconds, or hours. It is not the entire datetime expressed in that unit, it is the amount of that unit indivisible by the next larger unit. – Servy Commented Mar 9 at 1:42
  • 2 @Servy: I think when we're talking "divisions within a single unit" it's a bit more ambiguous. After all, you could say that the next largest unit up from nanosecond, in .NET, is "tick", in which case the Nanosecond property would always return 0. I'd also say that "nanosecond-of-microsecond" is probably useful less often than "nanosecond-of-second". But with this ambiguity, that's why in Noda Time we make it unambiguous, with NanosecondOfSecond. (We don't have NanosecondOfMicrosecond at all, but that's what we would call what DateTime.Nanosecond expresses, if we had it...) – Jon Skeet Commented Mar 9 at 6:46
 |  Show 2 more comments

3 Answers 3

Reset to default 4

The 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: 123
  • Microsecond: 456
  • Nanosecond: 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

相关推荐

  • Understanding C# DateTime MicroSecondNanoSeconds? - Stack Overflow

    Look at the docs for DateTime.Millisecond says the value can be 0-999 which makes complete sense.Howev

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信