Ask A User For Date Input With C# 9

This blog post will show you how to ask a user for date input in C# 9. This is important because the computer needs information before it can process anything else. We will be showing a code example so that you can get an idea of your best option depending on what type of program you are making.

A user can be asked for input of many different types of data. It is important to understand the difference between each of these types in order to ask for information correctly. Some of the most common are listed below:

Int (Integer) – This will contain whole numbers, without decimal places

Double (Real Number) – This will contain numbers with decimal places

String – This will contain text, such as names and addresses

DateTime (Date) – This will instruct the computer to ask for a date and time, which it will interpret as this type of data.

Boolean (Logic) – Asks whether or not something is true or false. When programming, “Boolean” is also used as another term for “Logic”.

Object – This will hold any type of data, which must be specifically programmed to use this. There are only a few occasions where this has been necessary.

Code

In the following code example, We are asking the user to input a valid date in en-au format and run all the check the validate that the date is valid before passing the code into an if statement.

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-au");
Console.WriteLine("Enter a date: ");
var temp = Console.ReadLine();
DateTime userDateInput;
DateTime.TryParse(temp, out userDateInput);

if(!userDateInput.Equals(DateTime.MinValue))
{
// Some code 
}


Posted

in

,

by