检查使用DateTime.TryParse多种日期格式() [英] Check several date formats using DateTime.TryParse()
本文介绍了检查使用DateTime.TryParse多种日期格式()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的方法来验证文本框。
I'm using a method to validate textboxes.
public bool ValidateDateTimeTextBoxes(params TextBox[] textBoxes)
{
DateTime value = DateTime.Today;
//string dateFormat = "dd/mm/yyyy";
foreach (var textBox in textBoxes)
{
if (!DateTime.TryParse(textBox.Text, out value))
{
return false;
}
}
return true;
}
我要检查的格式了。它要求 MM / DD / YYYY
,但我希望它是 DD / MM / YYYY
推荐答案
DateTime dt;
DateTime.TryParseExact(textBox.Text,
"dd/MM/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dt);
如果您要检查多种格式,你在你的问题更新,那么你可以做使用其他重载方法的 TryParseExact
这需要格式
参数作为字符串数组。
If you want to check multiple formats as you updated in your question then you can do using another overload method of TryParseExact
which takes format
parameter as array of string.
string[] formats = { "dd/MM/yyyy", "MM/dd/yyyy" };
DateTime.TryParseExact(txtBox.Text,
formats,
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out value));
请照顾格式字符串。正如你所提到的格式为 DD / MM / YYYY
。在这里,毫米
表示分钟
不是一个月。使用 MM
本月表示。
Please take care of format string. As you have mentioned format as dd/mm/yyyy
. Here mm
represents the minute
not the month. Use MM
for the month representation.
这篇关于检查使用DateTime.TryParse多种日期格式()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文