Ian*_*ien 0 c# parsing integer
我有一个文本框,用户可以输入一年"YY",我使用以下方法解析它:
int year;
if (int.TryParse(txtYear.Text, out year))
{
args.Year = year;
}
Run Code Online (Sandbox Code Playgroud)
问题是TryParse会将'07'转换为'7',我的方法期望年份的格式为YYYY(例如输入"07"应为"2007").进行此转换的最佳方法是什么?
DateTime date;
if (DateTime.TryParseExact("07", "yy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
Console.WriteLine(date.Year);
}
Run Code Online (Sandbox Code Playgroud)
正如@Mark建议的那样注意2K问题.
我只想让用户将年份输入YYYY格式.