简体   繁体   English

.NET datetime从字符串转换为datetime时的毫秒精度问题

[英].NET datetime Millisecond precision issue when converting from string to datetime

Hi. 你好。 I am trying to convert an incoming datetime value that comes to our system in a string format. 我试图以字符串格式转换到我们系统的传入日期时间值。 It seems that when the precision of milliseconds is higher than 7, the datetime parsing in .NET does not seem to like the value and cannot convert/parse the value. 似乎当毫秒的精度高于7时,.NET中的日期时间解析似乎不喜欢该值,并且无法转换/解析该值。 I am a bit stuck on what to do for this? 我有点不知道该为此做些什么? My only current thought is there is a limit on the millisecond size and that anymore precision is not possible? 我目前唯一的想法是毫秒尺寸有限制,不再有精度吗? But I want to confirm this is the case rather than assume. 但我想证实这是事实,而不是假设。 Example: 例:

string candidateDateTimeString = "2017-12-08T15:14:38.123456789Z";
if (!success)
        {
            success = DateTime.TryParseExact(trayportDateTimeString, "yyyy-
MM-dd'T'HH:mm:ss.fffffffff'Z'",
                CultureInfo.InvariantCulture, dateTimeStyles, out dateTime);
        }

If I reduce the 'f' values down to just 7, then date time parsing works fine. 如果我将'f'值减少到只有7,那么日期时间解析工作正常。 Is there a limit? 有限制吗? Or am I doing something obvious wrong? 或者我做了一些明显错误的事情?

根据自定义日期和时间格式字符串文档,7是第二个分数的最大支持位数。

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. 在内部,所有DateTime值都表示为自0001年1月1日午夜12:00:00起经过的刻度数(100纳秒间隔的数量)。

https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

see also: https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings 另见: https//docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

Precision of date and time values is more complex than you might think. 日期和时间值的精确度比您想象的更复杂。 There are different levels of precision involved: 有不同程度的精确度:

Precision of DateTime DateTime精度

DateTime stores the number of ticks since 01.01.0001 00:00 as a 64 bit value. DateTime将自01.01.0001 00:00以来的刻度数存储为64位值。 One tick is 100 nanoseconds. 一个刻度是100纳秒。 Since this is the maximum precision that can be stored, it makes no sense to format to a precision higher than that. 由于这是可以存储的最大精度,因此格式化到高于此精度的精度是没有意义的。 You can just add as many zeros as you need to represent a higher precision. 您可以根据需要添加尽可能多的零来表示更高的精度。 If you need to represent shorter timespans than 100 nanoseconds, you need to use a different type, such as an Int64 with a custom tick size. 如果需要表示比100纳秒更短的时间间隔,则需要使用其他类型,例如具有自定义刻度尺寸的Int64

Precision of DateTime.Now DateTime.Now精度

When you call DateTime.Now , you get a much lower precision than DateTime can store. 当您调用DateTime.Now ,您获得的精度远低于DateTime可以存储的精度。 The exact value depends on the system clock, but it is usually in the milliseconds range. 确切的值取决于系统时钟,但通常在毫秒范围内。

Precision of Stopwatch Stopwatch精度

When you measure the time with Stopwatch , depending on your system, you might get the time from a high performance clock, which more precise than the clock used for DateTime.Now , but still less than 100 nanoseconds. 当您使用Stopwatch测量时间时,根据您的系统,您可能会从高性能时钟获得时间,该时钟比DateTime.Now使用的时钟更精确,但仍然小于100纳秒。 On a system without high performance clock, the precision is the one of the regular system clock. 在没有高性能时钟的系统上,精度是常规系统时钟之一。

Summary 摘要

Unless the value that you are parsing originates from a high precision clock (like an atomic clock), parsing it to the full precision of DateTime , makes not much practical sense. 除非您正在解析的值来自高精度时钟(如原子钟),否则将其解析为DateTime的完全精度,实际上没有多大意义。 And if it comes from such a source, you need to resort to a different data type to represent the value. 如果它来自这样的源,您需要使用不同的数据类型来表示值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM