Convert string to date

Posted on 2013-11-29
Medium Priority
Last Modified: 2013-12-04
How do I convert this string "20131016", YYYYMMDD to "10/16/2013" ?

Question by:Dovberman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 12

Expert Comment

by:Anuradha Goli
ID: 39685603
  string date = "20100102";
   DateTime datetime = DateTime.ParseExact(date, "dd/MM/yyyy", CultureInfo.InvariantCulture);

Open in new window


Author Comment

ID: 39685702
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?


Accepted Solution

Dovberman earned 0 total points
ID: 39685757
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);
LVL 11

Expert Comment

ID: 39689544
hi.. this is your solution

Use DateTime.ParseExact(). Something like:

string date = "20100102";
   DateTime datetime = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);

Author Closing Comment

ID: 39695005
This is how I made it work.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question