该值是有效的年份 C#

发布于 2024-08-13 11:26:38 字数 221 浏览 6 评论 0原文

使用 C# 检查字符串是否为有效年份的最佳方法是什么?

我目前有一个包含值 {'All','2009','2008'} 等的下拉列表,我想知道选择的是日期之一还是“All”字段。

目前我正在检查 bool isYearValid = (Year.ToLower() == "all") ? false : true;

如何检查该值是否为有效年份,以便不必对“全部”进行硬编码检查?

What's the best way to check if a string is a valid year using C#?

I currently have a dropdown list that contains the values {'All','2009','2008'} etc, and I want to know whether the selection is one of the dates or the 'All' field.

Currently I'm checking for bool isYearValid = (Year.ToLower() == "all") ? false : true;

How do I check whether the value is a valid year so that I don't have to have this hardcoded check for 'All'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

︶ ̄淡然 2024-08-20 11:26:38

您可以尝试将年份解析为整数:

int iYear = 0;

if (Int.TryParse(Year, out iYear))
{
  //You have a valid year inside iYear
  //If you are not sure about the dropdown list values,
  //you can of course do more checks for validity. For example:
  //if iYear is in a proper range
}

You can try parsing the year as an integer:

int iYear = 0;

if (Int.TryParse(Year, out iYear))
{
  //You have a valid year inside iYear
  //If you are not sure about the dropdown list values,
  //you can of course do more checks for validity. For example:
  //if iYear is in a proper range
}
念三年u 2024-08-20 11:26:38

鉴于年份只需是一个有效的数字,您可以只使用 int32.TryParse,然后根据您希望年份所处的范围简单地检查范围。

Given that a year simply needs to be a valid number, you could just use int32.TryParse and then simply check the range based on what range you want the year to be in.

木槿暧夏七纪年 2024-08-20 11:26:38

你可以使用:

        DateTime dateTime;
        var year = ...
        DateTime.TryParse(string.Format("1/1/{0}", year), out dateTime);

you could use:

        DateTime dateTime;
        var year = ...
        DateTime.TryParse(string.Format("1/1/{0}", year), out dateTime);
咆哮 2024-08-20 11:26:38

无论如何,这不是更好吗?

bool isYearValid = !string.Equals(Year, "all", StringComparison.OrdinalIgnoreCase);

Wouldn't this be better anyway?

bool isYearValid = !string.Equals(Year, "all", StringComparison.OrdinalIgnoreCase);
櫻之舞 2024-08-20 11:26:38

怎么样:

<asp:ListItem Value="1900" Text="All" />

这样您就不必在验证点手动检查“全部”。在实际实现时,您仍然需要检查 1900,但无论如何,现在肯定有办法避免使成为一个特殊的场景。

除此之外,DateTime.TryParse 是确定字符串表示形式是否为有效日期的正确方法。

How about:

<asp:ListItem Value="1900" Text="All" />

That way you don't have to manually check for "All" at the validation point. You still need to check for 1900, when actually implementing this, but there'd definitely be now way to avoid making that a special scenario, anyway.

Other than that, DateTime.TryParse is the proper way to find out if a string representation is a valid date.

屋檐 2024-08-20 11:26:38

好吧,没有人提到它,但为什么不定义一个“All”符号
某处并在下拉初始化和比较中使用它?

Well, no-one's mentioned it, but why not have a symbol for 'All' defined
somewhere and use that in both the drop down initialisation and the comparison?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文