Solved

Convert string to date

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

Thanks,
0
Comment
Question by:Dovberman
  • 3
5 Comments
 
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

0
 

Author Comment

by:Dovberman
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?

Thanks,
0
 

Accepted Solution

by:
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);
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 39689544
hi.. this is your solution

Use DateTime.ParseExact(). Something like:

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

Author Closing Comment

by:Dovberman
ID: 39695005
This is how I made it work.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now