简体   繁体   English

DateTime.TryParse还是Datetime.TryParseExact?

[英]DateTime.TryParse or Datetime.TryParseExact?

I am converting date and time from a string. 我正在从字符串转换日期和时间。 I append the date parts from my input like below 我从输入中添加日期部分,如下所示

string joinedString = txtMonth.Text.Trim() + "/" + txtDate.Text.Trim() + "/" + txtYear.Text.Trim();

Generally i convert the string like below 通常我将字符串转换如下

DateTime dt;
if (DateTime.TryParse(joinedString, out dt))
{
    lblOutputCulture.Text = dt.ToString();
}
else
{
    lblOutputCulture.Text = "Invalid Date Time!!!";
}

Here i can get the datetime if the culture and datetime format of the culture is same as my string pattern. 如果区域性的区域性和日期时间格式与我的字符串模式相同,则在这里我可以获得日期时间。 But if it is chnaged like 'dd-MM-yy' the conversion shoul failed. 但是,如果将其更改为“ dd-MM-yy”,则转换将失败。

To overcome this, we need to parse using InvariantCulture. 为了克服这个问题,我们需要使用InvariantCulture进行解析。 For this i found 2 methods 为此,我发现了两种方法

Method 1 : Using DateTime.TryParse 方法1:使用DateTime.TryParse

if (DateTime.TryParse(joinedString, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
    lblOutput.Text = dt.ToString();
}
else
{
    lblOutput.Text = "Invalid Date Time!!!";
}

Here the date time is successfully converted even if the datetime format of the system is changed. 即使更改了系统的日期时间格式,此处的日期时间也已成功转换。 Also the following advatages i) The date and month input is 1 digit or 2 digit ii) The year input is 2 digit or 4 digit 还有以下优点i)输入的日期和月份是1位或2位ii)输入的年份是2位或4位

Method 2: Using DateTime.TryParseExact 方法2:使用DateTime.TryParseExact

if (DateTime.TryParseExact(joinedString, "M/d/yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
    lblOutput.Text = dt.ToString();
}
else
{
    lblOutput.Text = "Invalid Date Time!!!";
}

Here also datetime is converted with out culture specific but strict to the format i gave in the tryparseexact statement. 在这里,datetime的转换没有特定于文化的限制,但严格遵循我在tryparseexact语句中给出的格式。

But the following are the issues for this i) If i gave 4 digit year input it shows invalid date and time. 但是以下是此问题:i)如果我输入了4位数的年份,则显示无效的日期和时间。 If i change the format to 'M/d/yyyy' i can't able to give 2 digit year number. 如果我将格式更改为“ M / d / yyyy”,则无法提供2位数字的年份。 Like if i add time part, i need to give time part mandatory. 就像我添加时间部分一样,我需要给时间部分强制性的。

So which one is better? 那么哪个更好呢? Please give a suggestion. 请给个建议。 But mostly i found the second method in all blogs and forums.. 但大多数情况下,我在所有博客和论坛中都找到了第二种方法。

Which one is correct? 哪一个是正确的?

I would use neither: 我不会使用:

try {
  DateTime dt = new DateTime(int.Parse(txtYear.Text),
                             int.Parse(txtMonth.Text),
                             int.Parse(txtDate.Text));
   lblOutput.Text = dt.ToString(); //Not sure why we'd do this, but whatever
} catch (ArgumentOutOfRangeException) {
   lblOutput.Text = "Invalid Date Time!!!";
}

This easily extends to including other parts, eg time, by selecting a different overload of the constructor . 通过选择构造函数的其他重载,这很容易扩展到包括其他部分,例如时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM