Maly
Maly

Reputation: 23

Convert datetime to date using c#

I have datetime like:

01-Jan-10 12:00:00 AM I want to get only 01-Jan-10.I do not know how to convert it. Anyone know help me please, Thanks,

Upvotes: 0

Views: 1045

Answers (3)

burning_LEGION
burning_LEGION

Reputation: 13460

use method DateTime.ToShortDateString(); or DateTime.ToLongDateString()

Upvotes: 0

Phillip Schmidt
Phillip Schmidt

Reputation: 8818

Like this:

DateTime dt = DateTime.Today; //or whatever
string dateString = dt.ToShortDateString();

You can also use ToString() to format it however you like. In your case, it would be:

string dateString = dt.ToString("dd-MMM-YY");

Upvotes: 0

Samuel Neff
Samuel Neff

Reputation: 74949

If you have a DateTime object and want to get the string in that format, use myDateTime.ToString("dd-MMM-yy"). If you have a DateTime object and want to return a new DateTime object that is just the date component, use DateTime.Date.

Upvotes: 4

Related Questions