我曾经想要这个确切的东西,尽管我们可以假设用户的意思,因为正确性并不重要(对于像 MS Excel 这样的东西,当你输入文本日期时间时)。
实际上,您可以从内置文化中收集所有可能的日期时间格式,以及您可以自己定义的许多自定义格式。我曾经想要一个非常全面的日期时间格式匹配。
我做了一些与nawfal 的回答非常相似的事情。这是一个将他的答案与我的大量格式相结合的解决方案:
static string[] GetDateTimeFormats()
{
var defaultFormats = CultureInfo.GetCultures(CultureTypes.AllCultures)
.SelectMany(x => x.DateTimeFormat.GetAllDateTimePatterns())
.Distinct(); //to speed up things
//discard some formats that're not worthy of consideration
var goodDefaultFormats = defaultFormats.Where(x => !IsPoorFormat(x) && !IsConflictingFormat(x));
var customFormats = GetCustomDateTimeFormats();
var allFormats = goodDefaultFormats.Concat(customFormats).ToArray();
//a technique to get comma separated time formats,
//for eg, from dd-MM-yyyy h:mm:ss tt we get -> dd-MM-yyyy, h:mm:ss tt
var moreCustomFormats = allFormats.Select(f => new
{
f,
i = f.IndexOf(" h", StringComparison.OrdinalIgnoreCase)
})
.Where(x => x.i >= 0)
.Select(x => new { x.f, c = x.f[x.i - 1], x.i })
.Where(x => !char.IsPunctuation(x.c) && x.c != 't')
.Select(x => x.f.Insert(x.i, ","));
allFormats = allFormats.Union(moreCustomFormats).ToArray(); //Union removes duplicates
return allFormats;
}
static bool IsPoorFormat(string format)
{
//all discardable formats in case any
string[] ignorables = { "HH", "MMMM yyyy", "MMMM, yyyy", "yyyy MMMM", "yyyy.M", "yyyy-MM",
"MMMM,yy", "MMMM, yy", "MMMM,yyyy", "MMMM, yyyy", "yyyy. MMMM" };
return ignorables.Contains(format);
}
//to remove conflicting date formats, for example,
//formats like MM-dd-yy, yy-MM-dd, dd-MM-yy etc can be conflicting
static bool IsConflictingFormat(string format)
{
//in this example we discard formats like M-d-yy, yy-MM-dd etc, but keep dd-MM-yy
//in case you want to keep MM-dd-yy, the placeholders array should be { 'd', 'y' },
//and similarly if the preferred format is yy-MM-dd, array should be { 'M', 'd' }
var placeholders = new[] { 'M', 'y' };
var separators = new[] { ' ', '.', '-', '/' };
var patterns = placeholders.Select(x => x.ToString())
.SelectMany(x => new[] { x, x + x })
.SelectMany(x => separators, (x, y) => x + y);
return patterns.Any(format.StartsWith);
}
static string[] GetCustomDateTimeFormats()
{
return new[]
{
"dddd, MMMM d, yyyy h:mm:ss tt",
"dddd, MMMM d, yyyy H:mm:ss",
"dddd, MMMM d, yyyy h:mm tt",
"dddd, MMMM d, yyyy H:mm",
"dddd, MMM d, yyyy h:mm:ss tt",
"dddd, MMM d, yyyy H:mm:ss",
"dddd, MMM d, yyyy h:mm tt",
"dddd, MMM d, yyyy H:mm",
"ddd, MMMM d, yyyy h:mm:ss tt",
"ddd, MMMM d, yyyy H:mm:ss",
"ddd, MMMM d, yyyy h:mm tt",
"ddd, MMMM d, yyyy H:mm",
"ddd, MMM d, yyyy h:mm:ss tt",
"ddd, MMM d, yyyy H:mm:ss",
"ddd, MMM d, yyyy h:mm tt",
"ddd, MMM d, yyyy H:mm",
"dddd, MMMM d yyyy h:mm:ss tt",
"dddd, MMMM d yyyy H:mm:ss",
"dddd, MMMM d yyyy h:mm tt",
"dddd, MMMM d yyyy H:mm",
"dddd, MMM d yyyy h:mm:ss tt",
"dddd, MMM d yyyy H:mm:ss",
"dddd, MMM d yyyy h:mm tt",
"dddd, MMM d yyyy H:mm",
"ddd, MMMM d yyyy h:mm:ss tt",
"ddd, MMMM d yyyy H:mm:ss",
"ddd, MMMM d yyyy h:mm tt",
"ddd, MMMM d yyyy H:mm",
"ddd, MMM d yyyy h:mm:ss tt",
"ddd, MMM d yyyy H:mm:ss",
"ddd, MMM d yyyy h:mm tt",
"ddd, MMM d yyyy H:mm",
///////////////////////////
"dddd, d MMMM, yyyy h:mm:ss tt",
"dddd, d MMMM, yyyy H:mm:ss",
"dddd, d MMMM, yyyy h:mm tt",
"dddd, d MMMM, yyyy H:mm",
"dddd, d MMM, yyyy h:mm:ss tt",
"dddd, d MMM, yyyy H:mm:ss",
"dddd, d MMM, yyyy h:mm tt",
"dddd, d MMM, yyyy H:mm",
"ddd, d MMMM, yyyy h:mm:ss tt",
"ddd, d MMMM, yyyy H:mm:ss",
"ddd, d MMMM, yyyy h:mm tt",
"ddd, d MMMM, yyyy H:mm",
"ddd, d MMM, yyyy h:mm:ss tt",
"ddd, d MMM, yyyy H:mm:ss",
"ddd, d MMM, yyyy h:mm tt",
"ddd, d MMM, yyyy H:mm",
"dddd, d MMMM yyyy h:mm:ss tt",
"dddd, d MMMM yyyy H:mm:ss",
"dddd, d MMMM yyyy h:mm tt",
"dddd, d MMMM yyyy H:mm",
"dddd, d MMM yyyy h:mm:ss tt",
"dddd, d MMM yyyy H:mm:ss",
"dddd, d MMM yyyy h:mm tt",
"dddd, d MMM yyyy H:mm",
"ddd, d MMMM yyyy h:mm:ss tt",
"ddd, d MMMM yyyy H:mm:ss",
"ddd, d MMMM yyyy h:mm tt",
"ddd, d MMMM yyyy H:mm",
"ddd, d MMM yyyy h:mm:ss tt",
"ddd, d MMM yyyy H:mm:ss",
"ddd, d MMM yyyy h:mm tt",
"ddd, d MMM yyyy H:mm",
/////////////////////////////////
"yyyy, MMMM d h:mm:ss tt",
"yyyy, MMMM d H:mm:ss",
"yyyy, MMMM d h:mm tt",
"yyyy, MMMM d H:mm",
"yyyy, MMM d h:mm:ss tt",
"yyyy, MMM d H:mm:ss",
"yyyy, MMM d h:mm tt",
"yyyy, MMM d H:mm",
"yyyy, MM d h:mm:ss tt",
"yyyy, MM d H:mm:ss",
"yyyy, MM d h:mm tt",
"yyyy, MM d H:mm",
"yyyy MMMM d h:mm:ss tt",
"yyyy MMMM d H:mm:ss",
"yyyy MMMM d h:mm tt",
"yyyy MMMM d H:mm",
"yyyy MMM d h:mm:ss tt",
"yyyy MMM d H:mm:ss",
"yyyy MMM d h:mm tt",
"yyyy MMM d H:mm",
"yyyy MM d h:mm:ss tt",
"yyyy MM d H:mm:ss",
"yyyy MM d h:mm tt",
"yyyy MM d H:mm",
///////////////////////
"yyyy, d MMMM h:mm:ss tt",
"yyyy, d MMMM H:mm:ss",
"yyyy, d MMMM h:mm tt",
"yyyy, d MMMM H:mm",
"yyyy, d MMM h:mm:ss tt",
"yyyy, d MMM H:mm:ss",
"yyyy, d MMM h:mm tt",
"yyyy, d MMM H:mm",
"yyyy, d MM h:mm:ss tt",
"yyyy, d MM H:mm:ss",
"yyyy, d MM h:mm tt",
"yyyy, d MM H:mm",
"yyyy d MMMM h:mm:ss tt",
"yyyy d MMMM H:mm:ss",
"yyyy d MMMM h:mm tt",
"yyyy d MMMM H:mm",
"yyyy d MMM h:mm:ss tt",
"yyyy d MMM H:mm:ss",
"yyyy d MMM h:mm tt",
"yyyy d MMM H:mm",
"yyyy d MM h:mm:ss tt",
"yyyy d MM H:mm:ss",
"yyyy d MM h:mm tt",
"yyyy d MM H:mm",
////////////////////////////////
"MMMM d, yyyy h:mm:ss tt",
"MMMM d, yyyy H:mm:ss",
"MMMM d, yyyy h:mm tt",
"MMMM d, yyyy H:mm",
"MMM d, yyyy h:mm:ss tt",
"MMM d, yyyy H:mm:ss",
"MMM d, yyyy h:mm tt",
"MMM d, yyyy H:mm",
"MMMM d yyyy h:mm:ss tt",
"MMMM d yyyy H:mm:ss",
"MMMM d yyyy h:mm tt",
"MMMM d yyyy H:mm",
"MMM d yyyy h:mm:ss tt",
"MMM d yyyy H:mm:ss",
"MMM d yyyy h:mm tt",
"MMM d yyyy H:mm",
////////////////////////////////////
"d MMMM, yyyy h:mm:ss tt",
"d MMMM, yyyy H:mm:ss",
"d MMMM, yyyy h:mm tt",
"d MMMM, yyyy H:mm",
"d MMM, yyyy h:mm:ss tt",
"d MMM, yyyy H:mm:ss",
"d MMM, yyyy h:mm tt",
"d MMM, yyyy H:mm",
"d MMMM yyyy h:mm:ss tt",
"d MMMM yyyy H:mm:ss",
"d MMMM yyyy h:mm tt",
"d MMMM yyyy H:mm",
"d MMM yyyy h:mm:ss tt",
"d MMM yyyy H:mm:ss",
"d MMM yyyy h:mm tt",
"d MMM yyyy H:mm",
/////////////////////////
"dddd, MMMM d, yyyy",
"dddd, MMM d, yyyy",
"ddd, MMMM d, yyyy",
"ddd, MMM d, yyyy",
"dddd, MMMM d yyyy",
"dddd, MMM d yyyy",
"ddd, MMMM d yyyy",
"ddd, MMM d yyyy",
//////////////////////////
"dddd, d MMMM, yyyy",
"dddd, d MMM, yyyy",
"ddd, d MMMM, yyyy",
"ddd, d MMM, yyyy",
"dddd, d MMMM yyyy",
"dddd, d MMM yyyy",
"ddd, d MMMM yyyy",
"ddd, d MMM yyyy",
///////////////////////////
"MMMM d, yyyy",
"MMM d, yyyy",
"MMMM d yyyy",
"MMM d yyyy",
//////////////////////////
"d MMMM, yyyy",
"d MMM, yyyy",
"d MMMM yyyy",
"d MMM yyyy",
//////////////////////////
"yyyy, MMMM d",
"yyyy, MMM d",
"yyyy MMMM d",
"yyyy MMM d",
//////////////////////////
"yyyy d MMMM",
"yyyy d MMM",
"yyyy, d MMMM",
"yyyy, d MMM",
///////////////////////////
"d MMMM",
"d MMM",
/////////////////////////////
"MMMM d",
"MMM d",
////////////////////////////
"dd",
};
}