Solved

Convert string to date

Posted on 2013-11-29
5
390 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Make all values in arrayist to titlecase+vb.net 23 27
ADO.NET ENTITY DATA MODEL 3 30
C# Single Form 8 26
Vb. Net application freezes 9 27
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

22 Experts available now in Live!

Get 1:1 Help Now