我对这个问题完全不知所措。据我所见,文档和我在SO上看到的其他帖子都说这应该可行。我一定是错过了一些愚蠢的东西,但我就是看不到它。
以下代码行会导致"字符串未被识别为有效的DateTime"的FormatException异常:
以下代码行会导致"字符串未被识别为有效的DateTime"的FormatException异常:
return DateTime.ParseExact(value, DateFormat, null,
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal);
value
的值为"11/04/2013"
DateFormat
为"dd/MM/yyyy"
- 当前区域设置为
en-GB
- 我尝试了各种不同的
DateTimeStyles
变体,但都没有效果。
我的初衷是使用格式ddd, dd/MMM/yyyy
,但那也不起作用(在那种情况下的值为Tue, 30/Apr/2013
)
我还尝试通过传递new CultureInfo("en-GB")
而不是null
来强制使用en-GB区域设置。
我还将代码提取到自己的控制台应用程序中,以查看环境是否有所不同(ASP.NET MVC 3)。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var DateFormat = "dd/MM/yyyy";
var value = "30/04/2013";
var culture = new CultureInfo("en-GB");
var result = DateTime.ParseExact(value, DateFormat, culture,
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.AssumeUniversal);
Console.WriteLine("\"{0}\" as \"{1}\" ==> {2}", value, DateFormat, result);
Console.ReadKey();
}
}
}
这仍然给我同样的错误。
Thread.CurrentThread.CurrentCulture
和Thread.CurrentThread.CurrentUICulture
的值是什么? - Daniel Hilgarth