Reputation: 49
I have to code program, which will asks user about some data and then will print some text with this data. Below is just piece of code (which works), there are others variables too.
class Student
{
static public DateTime birthDay;
public void GetStudentInformation()
{
Console.WriteLine("Enter student's birth date (as mm/dd/yyyy): ");
birthDay = DateTime.Parse(Console.ReadLine());
}
public void PrintStudentData(ref DateTime birthDay)
{
Console.WriteLine("Student was born in {0}", birthDay.ToString("d"));
}
}
class Program
{
static void Main(string[] args)
{
Student newStudent = new Student();
newStudent.GetStudentInformation();
newStudent.PrintStudentData(ref Student.birthDay);
Console.ReadKey()
}
}
When I'm asking about birthday I need only date, not exactly time. There are questions:
I want to add I'm really beginner in C# and tried some code with CultureInfo
, ParseExact
, TryParse
, and modyfing output string with {0:'dd/mm/yyyy'}
.
Upvotes: 0
Views: 8903
Reputation: 8025
This let user input in format day/month/year
and output as dd.MM.yyyy
static void Main()
{
Student newStudent = new Student();
newStudent.GetStudentInformation();
newStudent.PrintStudentData(ref Student.birthDay);
Console.ReadKey();
logEnd();
}
class Student
{
static public DateTime birthDay;
public void GetStudentInformation()
{
Console.WriteLine("Enter student's birth date as day/month/year");
string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/M/yyyy", "d/MM/yyyy",
"dd/MM/yy", "dd/M/yy", "d/M/yy", "d/MM/yy"};
while (!DateTime.TryParseExact(Console.ReadLine(), formats,
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None,
out birthDay))
{
Console.WriteLine("Your input is incorrect. Please input again.");
}
// User input correct, birthDay can now be used
}
public void PrintStudentData(ref DateTime birthDay)
{
Console.WriteLine("Student was born in {0}", birthDay.ToString("dd.MM.yyyy"));
}
}
Upvotes: 0
Reputation: 98760
Sounds like DateTime.TryParseExact
is a good way to do it.
Converts the specified string representation of a date and time to its DateTime equivalent using the specified format, culture-specific format information, and style. The format of the string representation must match the specified format exactly. The method returns a value that indicates whether the conversion succeeded.
DateTime birthDay;
if(DateTime.TryParseExact(Console.ReadLine(), "MM/dd/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None, out birthDay)
{
// Your input string can (and will) be parsed with MM/dd/yyyy format.
}
else
{
// Invalid format or value.
}
By the way, I changed your mm
to MM
because mm
specifier is for minutes but MM
specifier is for months.
For your questions;
How to change that input date by user would be in other format than mm/dd/yyy?
You can't. This might create a lot of ambiguous situations like what is the format of 01/02/2016
? Is it dd/MM/yyyy
or MM/dd/yyyy
? This totally depends on where you live and which culture settings you use.
How I would operate on output format date? So it wouldn't be yyyy-dd-mm but dd/mm/yyyy or dd.mm.yyyy?
With output, if you mean the Console.WriteLine
part, this The "d"
standard format specifier uses ShortDatePattern
of your CurrentCulture
settings where you run this code. That means the output format depends on the current culture settings. If this property has dd/MM/yyyy
, you will be fine. If it is not, you should format it with customd date format specifiers like;
Console.WriteLine("Student was born in {0}",
birthDay.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture));
Upvotes: 1