Magz
Magz

Reputation: 15

Convert date issue in c#

I have dateformat issue where I need to convert the date from DB to different formats according to the login

Like I have 3 different format

DD/MM/YY,MM/DD/YY,DD/MMM/YY I tried to convert using the following methods but no luck.

DateTime.TryParseExact(drtemp["StartDate"].ToString(),dateFormat,null,
   System.Globalization.DateTimeStyles.None,out startDate);
orderDate =DateTime.ParseExact(drtemp["StartDate"].ToString(), dateFormat, null);

Any help is highly apprenticed Thanks, Magz

Upvotes: 1

Views: 656

Answers (3)

FIre Panda
FIre Panda

Reputation: 6637

Try

string date = DateTime.Now.ToString("dd/MM/yy");
string date1 = DateTime.Now.ToString("MM/dd/yy");
string date2 = DateTime.Now.ToString("dd/MMM/yy");

In your case you could do

string formattedDate = drtemp["StartDate"].ToString("dd/MM/yy");

Upvotes: 0

Eduard
Eduard

Reputation: 3216

Or You could do this:

String.Format(dateFormat, (DateTime)drtemp["StartDate"])

Upvotes: 0

AdaTheDev
AdaTheDev

Reputation: 147354

You can do:

string formattedDate = ((DateTime)drtemp["StartDate"]).ToString(dateFormat);

See MSDN DateTime.ToString reference

Upvotes: 3

Related Questions