Reputation: 3
Here em taking input from user and then convert into DateTime when it is print on output screen it show Date and time both but i want to show only Date not time how can i do this??
//INPUT
Console.Write("Pleas Enter Date of Birth: mm-dd-yy: ");
Date_Of_Birth = Console.ReadLine();
//Converstion
Date_Only = Convert.ToDateTime(Date_Of_Birth);
//Printing no ouput Screen.
Console.Write(Date_Only);"
Upvotes: 1
Views: 593
Reputation: 183
You can easily convert the datetime object to either date or time
Just replace DateTime.Now with your date object.
Use this for date only
Console.Write(DateTime.Now.ToShortDateString());
Use this for time only
Console.Write(DateTime.Now.ToShortDateString());
Upvotes: 0
Reputation: 18142
Console.Write
has its own formatter built in:
Console.Write("{0:MM-dd-yy}", Date_Only);
You may also consider using Console.WriteLine
rather than Console.Write
if you'd like it to place a line feed after the print:
Console.WriteLine("{0:MM-dd-yy}", Date_Only);
Upvotes: 0
Reputation: 11957
You can control how DateTime is formatted when printed, by using .ToString("Format")
method:
Console.Write(dateOnly.ToString("MM-dd-yy"));
or use .ToShortDateString()
or .ToLongDateString()
which will format the date using current culture/language settings.
Upvotes: 1