以下面的格式输入。 [英] Take input in the follwing format.
本文介绍了以下面的格式输入。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string input;
DateTime db;
Console.WriteLine("Enter Date in this Format(DD-MM-YYYY):");
input = Console.ReadLine();
我尝试过:
我试图以给定的格式输入日期,但它给我格式错误我该如何解决?
What I have tried:
I tried to enter the date in given format but it gives me format error how can i solve this?
推荐答案
试试这个
try this
DateTime db;
Console.WriteLine("Enter Date in this Format(DD-MM-YYYY):");
string input = Console.ReadLine();
string[] formats = { "d-M-yyyy", "d-MM-yyyy", "dd-M-yyyy", "dd-MM-yyyy" };
db = DateTime.ParseExact(input, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
用于解析验证使用此
for parsing with validation use this
if (DateTime.TryParseExact(input, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out db))
{
DateTime date = db;
}
这篇关于以下面的格式输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文