C# 解析时间戳格式为“yyyyMMdd HH:mm:SS.ms”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9902535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 11:08:30  来源:igfitidea点击:

C# parse timestampwith format "yyyyMMdd HH:mm:SS.ms"

c#datetime-format

提问by Abstract

I wanted to format a string to dateTime with the format

我想用格式将字符串格式化为 dateTime

"yyyyMMdd HH:mm:SS.ms"

I tried doing "yyyyMMdd HH:mm:SS"as the string format for ParseExactbut it doesn't recognise it. Also no clue how to include the milliseconds as well

我尝试将其"yyyyMMdd HH:mm:SS"用作字符串格式,ParseExact但它无法识别。也不知道如何包括毫秒

any assistance?

任何帮助?

采纳答案by Jon Skeet

From the custom date and time format stringspage, you use ssfor seconds, and FFFfor milliseconds:

自定义日期和时间格式字符串页面,您使用ss秒和FFF毫秒:

"yyyyMMdd HH:mm:ss.FFF"

or

或者

"yyyyMMdd HH:mm:ss.fff"

(Use the first if trailing 0s are suppressed, the second otherwise.)

(如果尾随 0 被抑制,则使用第一个,否则使用第二个。)