如何验证闰年的 DateTime
How to validate DateTime for a leap year
我正在使用 C# 并尝试查找给定的日期和月份是否对闰年有效。这是我的代码:
static void Main(string[] args)
{
Console.WriteLine("The following program is to find whether the Date and Month is Valid for an LEAP YEAR");
Console.WriteLine("Enter the Date");
int date = Convert.ToInt16(Console.ReadLine()); //User values for date and month
Console.WriteLine("Enter the Month");
int month = Convert.ToInt16(Console.ReadLine());
{
if (month == 2 && date < 30) //Determination of month and date of leap year using If-Else
Console.WriteLine("Your input is valid");
else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && date < 32)
Console.WriteLine("Your inpput valid1");
else if (( month == 4 || month == 6 || month == 9 || month == 11 ) && date < 31)
Console.WriteLine("Your inpput valid2");
else
Console.WriteLine("Your input INvalid");
Console.ReadKey();
}
}
我的问题是,我可以为这个程序使用 DateTime
还是更好的方法?欢迎提出任何建议。
我建议将输入作为 string
然后使用 DateTime.TryParse
method. DateTime.TryParse
accepts a string
and an out DateTime
(out keyword) 和 returns true
如果字符串输入都被正确解析并且是一个有效的 DateTime
,否则 false
。
来自文档:
If s is the string representation of a leap day in a leap year in the current calendar, the method parses s successfully. If s is the string representation of a leap day in a non-leap year in the current culture's current calendar, the parse operation fails and the method returns false.
用法示例:
Console.WriteLine("Please enter a date.");
string dateString = Console.ReadLine();
DateTime dateValue;
if (DateTime.TryParse(dateString, out dateValue))
{
// Hooray, your input was recognized as having a valid date format,
// and is a valid date! dateValue now contains the parsed date
// as a DateTime.
Console.WriteLine("You have entered a valid date!");
}
else
{
// Aww, the date was invalid.
Console.WriteLine("The provided date could not be parsed.");
}
年份部分使用已知的闰年,例如2000 并附加月、日和年以形成类似于 mm-dd-2000 的字符串,其中 mm
和 dd
是用户输入的值。然后使用 DateTime.TryParse 方法,如果日期有效,则 returns 为真。
您可以使用 DateTime.DaysInMonth
作为已知闰年的年份,例如 2016。
if (month >= 1 && month <= 12 && date >= 1 && date <= DateTime.DaysInMonth(2016, month))
Console.WriteLine("Your input is valid");
else
Console.WriteLine("Your input is invalid");
如果您从不同的部分开始工作,那么只需:
try
{
new DateTime(year, month, day);
}
catch (ArgumentOutOfRangeException)
{
// it's not valid
}
不过,如果您不想依赖异常,那么请使用 juharr 的回答,使用 DateTime.DaysInMonth
。
我正在使用 C# 并尝试查找给定的日期和月份是否对闰年有效。这是我的代码:
static void Main(string[] args)
{
Console.WriteLine("The following program is to find whether the Date and Month is Valid for an LEAP YEAR");
Console.WriteLine("Enter the Date");
int date = Convert.ToInt16(Console.ReadLine()); //User values for date and month
Console.WriteLine("Enter the Month");
int month = Convert.ToInt16(Console.ReadLine());
{
if (month == 2 && date < 30) //Determination of month and date of leap year using If-Else
Console.WriteLine("Your input is valid");
else if ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && date < 32)
Console.WriteLine("Your inpput valid1");
else if (( month == 4 || month == 6 || month == 9 || month == 11 ) && date < 31)
Console.WriteLine("Your inpput valid2");
else
Console.WriteLine("Your input INvalid");
Console.ReadKey();
}
}
我的问题是,我可以为这个程序使用 DateTime
还是更好的方法?欢迎提出任何建议。
我建议将输入作为 string
然后使用 DateTime.TryParse
method. DateTime.TryParse
accepts a string
and an out DateTime
(out keyword) 和 returns true
如果字符串输入都被正确解析并且是一个有效的 DateTime
,否则 false
。
来自文档:
If s is the string representation of a leap day in a leap year in the current calendar, the method parses s successfully. If s is the string representation of a leap day in a non-leap year in the current culture's current calendar, the parse operation fails and the method returns false.
用法示例:
Console.WriteLine("Please enter a date.");
string dateString = Console.ReadLine();
DateTime dateValue;
if (DateTime.TryParse(dateString, out dateValue))
{
// Hooray, your input was recognized as having a valid date format,
// and is a valid date! dateValue now contains the parsed date
// as a DateTime.
Console.WriteLine("You have entered a valid date!");
}
else
{
// Aww, the date was invalid.
Console.WriteLine("The provided date could not be parsed.");
}
年份部分使用已知的闰年,例如2000 并附加月、日和年以形成类似于 mm-dd-2000 的字符串,其中 mm
和 dd
是用户输入的值。然后使用 DateTime.TryParse 方法,如果日期有效,则 returns 为真。
您可以使用 DateTime.DaysInMonth
作为已知闰年的年份,例如 2016。
if (month >= 1 && month <= 12 && date >= 1 && date <= DateTime.DaysInMonth(2016, month))
Console.WriteLine("Your input is valid");
else
Console.WriteLine("Your input is invalid");
如果您从不同的部分开始工作,那么只需:
try
{
new DateTime(year, month, day);
}
catch (ArgumentOutOfRangeException)
{
// it's not valid
}
不过,如果您不想依赖异常,那么请使用 juharr 的回答,使用 DateTime.DaysInMonth
。