Convert string to date

How do I convert this string "20131016", YYYYMMDD to "10/16/2013" ?

Thanks,
DovbermanAsked:
Who is Participating?
 
DovbermanConnect With a Mentor Author Commented:
I have always had issues with string to date conversion.

This is how I made it work:

    strAvailDate =  "20131016"

                string strYear = strAvailDate.Substring(0,4) ; //"2013"
                string strMonth = strAvailDate.Substring(4,2) ; //"10"
                string strDay  = strAvailDate.Substring(6,2); // "16"
             
                strAvailDate = strMonth + "/" + strDay +"/" + strYear;

                DateTime dteAvailDate = Convert.ToDateTime(strAvailDate);
0
 
Anuradha GoliSystems Development / Support SpecialistCommented:
  string date = "20100102";
   DateTime datetime = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Open in new window

0
 
DovbermanAuthor Commented:
string strAvailDate =  "20131016";
DateTime dteCalSelected = DateTime.ParseExact(strAvaiDate, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Error: String was not recognized as a valid DateTime.

What should I change?

Thanks,
0
 
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. this is your solution

Use DateTime.ParseExact(). Something like:

string date = "20100102";
   DateTime datetime = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);
0
 
DovbermanAuthor Commented:
This is how I made it work.
0
All Courses

From novice to tech pro — start learning today.