gpt4 book ai didi

c# - 从 C# 中的字符串检查日期格式

转载 作者:可可西里 更新时间:2023-11-01 03:04:55 26 4
gpt4 key购买 nike

我想检查 string 是否包含日期,例如 1/01/200010/01/2000 in dd/MM/yyyy 格式。

到目前为止,我已经尝试过了。

DateTime dDate = DateTime.Parse(inputString);
string.Format("{0:d/MM/yyyy}", dDate);

但是我如何检查该格式是否正确以抛出异常

最佳答案

string inputString = "2000-02-02";
DateTime dDate;

if (DateTime.TryParse(inputString, out dDate))
{
String.Format("{0:d/MM/yyyy}", dDate);
}
else
{
Console.WriteLine("Invalid"); // <-- Control flow goes here
}

关于c# - 从 C# 中的字符串检查日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19019718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com