在 C# 中将 dd/MM/yyyy hh:mm:ss.fff 从 String 转换为 DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951004/
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-06 22:08:38  来源:igfitidea点击:

Convert dd/MM/yyyy hh:mm:ss.fff from String to DateTime in C#

c#asp.netdatetime

提问by soldieraman

I am trying to convert a string of the below format dd/MM/yyyy hh:mm:ss.fff into DateTime value

我正在尝试将以下格式的字符串 dd/MM/yyyy hh:mm:ss.fff 转换为 DateTime 值

easiest way?

最简单的方法?

BTW

顺便提一句

IFormatProvider culture = new CultureInfo("en-US", true);
DateTime.ParseExact(timeString, "dd/MM/yyyy hh:mm:ss.fff",culture);

Throws an invalid time exception

抛出一个无效的时间异常

Eg. 11/12/2009 13:30:00.000 Where 12 is the month (i know weird)

例如。11/12/2009 13:30:00.000 其中 12 是月份(我知道很奇怪)

采纳答案by Adriaan Stander

You have to use HH

你必须使用HH

string timeString = "11/12/2009 13:30:00.000";
IFormatProvider culture = new CultureInfo("en-US", true); 
DateTime dateVal = DateTime.ParseExact(timeString, "dd/MM/yyyy HH:mm:ss.fff", culture);

回答by Anton Gogolev

hh:mm:ss.fffshould be HH:mm:ss.fffsince you're using 24-hour format.

hh:mm:ss.fff应该是HH:mm:ss.fff因为您使用的是 24 小时格式。