Link to home
Start Free TrialLog in
Avatar of rajesh_khater
rajesh_khater

asked on

Converting a string from one date format to another

I have a string which contains date in yyyymmdd format, eg: 20050415.

I need to convert it to a string in dd-MMM-yyyy format eg: 15-Apr-2007.

What is the best way to do this?
Avatar of YZlat
YZlat
Flag of United States of America image

use String.Format
try this. just make sure that datefield is of type date

String.Format("{0:dd-MMM-yyyy}", datefield)
Avatar of rajesh_khater
rajesh_khater

ASKER

it is not of type date, as I already told.
string mystr="20050415";

DateTime dt = DateTime.Parse (long.Parse (mystr).ToString());

string mynewstring=String.Format("{0:dd-MMM-yyyy}", dt)
long.parse will take it as long value 20050415, which is not correct. DateTime.Parse interprets long value as a certain offset from a base date, i think.
long.Parse(mystr).ToString() is equal to mystr only ! That will not help at all.
SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
my solution works, I tested it