Reputation: 15
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
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
Reputation: 3216
Or You could do this:
String.Format(dateFormat, (DateTime)drtemp["StartDate"])
Upvotes: 0
Reputation: 147354
You can do:
string formattedDate = ((DateTime)drtemp["StartDate"]).ToString(dateFormat);
See MSDN DateTime.ToString reference
Upvotes: 3