Solved

Convert string to date

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

Thanks,
0
Comment
Question by:Dovberman
[X]
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
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

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…
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…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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