我需要让用户输入一个日期时间格式,但我需要对其进行验证以检查其是否可接受。用户可能会输入"yyyy-MM-dd"并且一切正常,但他们也可以输入"MM/yyyyMM/ddd"或任何其他组合。是否有一种方法可以验证这个格式?
我需要让用户输入一个日期时间格式,但我需要对其进行验证以检查其是否可接受。用户可能会输入"yyyy-MM-dd"并且一切正常,但他们也可以输入"MM/yyyyMM/ddd"或任何其他组合。是否有一种方法可以验证这个格式?
你是在寻找类似这样的东西吗?
DateTime expectedDate;
if (!DateTime.TryParse("07/27/2012", out expectedDate))
{
Console.Write("Luke I am not your datetime.... NOOO!!!!!!!!!!!!!!");
}
如果您的用户知道所需格式的确切内容...string[] formats = { "MM/dd/yyyy", "M/d/yyyy", "M/dd/yyyy", "MM/d/yyyy" };
DateTime expectedDate;
if (!DateTime.TryParseExact("07/27/2012", formats, new CultureInfo("en-US"),
DateTimeStyles.None, out expectedDate))
{
Console.Write("Thank you Mario, but the DateTime is in another format.");
}
我不知道是否有任何方法可以实际验证用户输入的格式,因为有时您想故意包含可以转换为任何字符的字符。您可以考虑的一件事是允许用户通过显示其输入格式的预览来进行自我验证。
DateTime.Now.ToString("This is made up and won't work");
这样的操作,预览将显示 T1i30 i30 14a4e up an4 will noP work
。你可以使用 try/catch
块来处理任何非常奇怪的情况。 - Jason我假设您想知道指定的格式字符串是否有效...
为此,您可以回传它:
private bool IsValidDateFormat(string dateFormat)
{
try
{
String dts=DateTime.Now.ToString(dateFormat, CultureInfo.InvariantCulture);
DateTime.ParseExact(dts, dateFormat, CultureInfo.InvariantCulture);
return true;
}
catch (Exception)
{
return false;
}
}
除非我记错了,仅一个字符长的DateTime格式字符串是无效的。您可以假设任何2个或更多字符的DateTime格式字符串都是有效的。
DateTime.ParseExact("qq", "qq", null) == DateTime.Today
DateTime.ParseExact("myy", "501", null) == "05/01/2001"
参考,允许用作格式的单个字符字符串:
d,D,f,F,g,G,m,M,o,O,r,R,s,T,u,U,y,Y
q
)都是无效的。所有其他字符串都将被成功解析为格式化字符串。DateTime.Now.ToString("%x") == "x"
。 - Markus Jarderoty
视为h
并获得奇怪的结果。 - Guvanteusing System.Globalization;
var format = "aaaaa";
try {
var dt = DateTime.ParseExact(
DateTime.Now.ToString(format, CultureInfo.InvariantCulture),
format,
CultureInfo.InvariantCulture,
DateTimeStyles.NoCurrentDateDefault);
return dt != default;
} catch {
return false;
}
/*
"aaaaa" -> false
"h" -> false
"hh" -> true
"fff" -> true
"gg" -> false
"yyyy gg" -> true
"'timezone: 'K" -> false
"zzz" -> false
*/
static private bool IsValidDateFormat(string dateFormat)
{
try
{
DateTime pastDate = DateTime.Now.Date.Subtract(new TimeSpan(10, 0, 0, 0, 0));
string pastDateString = pastDate.ToString(dateFormat, CultureInfo.InvariantCulture);
DateTime parsedDate = DateTime.ParseExact(pastDateString, dateFormat, CultureInfo.InvariantCulture);
if (parsedDate.Date.CompareTo(pastDate.Date) ==0)
{
return true;
}
return false;
}
catch
{
return false;
}
}
我使用这段代码 - 这是对Shapiro Yaacov发布的代码进行修改。 看起来"DateTime.ParseExact"在使用无效的日期格式字符串时不会抛出异常 - 它只会返回"DateTime.Now"。 我的方法是将过去的日期转换为字符串,然后检查ParseExact()是否返回该字符串。
你没有谈论你的验证策略。无论如何,你应该使用涉及正则表达式的东西,然后应用允许的模式。这将有助于防止形式上的有效性..然后你必须关心实际内容,并确保值根据月份、日期和年份是正确的。
无论如何,有几个人建议使用DateTime.TryParse()方法让基质为你处理。但你仍然必须指定格式!所以没有魔法!否则你会陷入歧义
这对我有效 -
try
{
String formattedDate = DateTime.Now.ToString(dateFormat);
DateTime.Parse(formattedDate);
return true;
}
catch (Exception)
{
return false;
}