我在日志中编写一个程序的事件,每行都附有时间戳,格式如下:
DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss FFF")
我原以为毫秒部分总是有 3 位数字。但是出现以下内容:
11/29/2013 18:53:14 96 - SessionKey-2rvun1agyw1svcexmqf5dejh-MAC.Master Page_Load - Begin
11/29/2013 18:53:14 975 - SessionKey-2rvun1agyw1svcexmqf5dejh-MAC.Master Page_Load - End
第一个为什么毫秒部分不是096?
最佳答案
来自 "FFF" Custom Format Specifier
The "FFF" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value. However, trailing zeros or three zero digits are not displayed.
编辑:好的,我想我明白这里发生了什么。原来,OP的毫秒部分是960
不是096
.
这就是为什么它完全按照 MSDN 页面中的描述工作。
尾随0
在 960
的末尾, 它不领先 0
在096
.
作为解决方案,您可以使用 The "fff" Custom Format Specifier
反而。在我看来,它不尾随或前导零。至少它没有写在MSDN 页面:)
EDIT2:我认为人们混淆了 trailing zero 是什么和 leading zero
来自维基百科页面;
Trailing zero
In mathematics, trailing zeros are a sequence of 0s in the decimal representation (or more generally, in any positional representation) of a number, after which no other digits follow.
Leading zero
A leading zero is any 0 digit that leads a number string in positional notation. For example, James Bond's famous identifier, 007, has two leading zeros.
关于c# - 以毫秒为单位创建日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20287851/