C#无法获取String到DateTime转换工作 [英] C# Can't get String to DateTime conversion to work
本文介绍了C#无法获取String到DateTime转换工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用DateTime.ParseExact()之前,我已经做了一些简单的字符串 - > DateTime转换,但是我有一个字符串,我似乎无法正确解析。我可能正在做一些非常明显的错误,但我看不到它是什么。
I've done some simple string -> DateTime conversions before using DateTime.ParseExact(), but I have a string that I can't seem to get parsed properly. I'm probably doing something very obvious wrong but I just can't see what it is.
代码如下:
string date = "Tue Jun 23, 2009 2:23 pm";
DateTime lastupdate = DateTime.ParseExact(date, "ddd MMM dd, yyyy h:mm tt", null);
运行它会给出一个FormatException。我的格式化字符串不正确吗?
Running it gives a FormatException. Is my formatting string incorrect?
ps
我试图在输入字符串中使用pm而不是pm,但是也没有帮助。 p>
ps I've tried to use p.m. rather than pm in the input string but that didn't help either.
推荐答案
尝试这样:
DateTime lastupdate = DateTime.ParseExact(date, "ddd MMM dd, yyyy h:mm tt", new System.Globalization.CultureInfo("en-us"));
如果文化是例如fr-fr或de-de 。
An error would occur if the culture was for example "fr-fr" or "de-de".
这篇关于C#无法获取String到DateTime转换工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文