Muhammd Waseem
Muhammd Waseem

Reputation: 3

How to remove time portion of DateTime in C# in DateTime object only?

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

Answers (3)

avinash
avinash

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

Khan
Khan

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

Marcin Deptuła
Marcin Deptuła

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

Related Questions