Solved

Convert String to date format

Posted on 2011-09-15
13
285 Views
Last Modified: 2012-06-22
Hi There,

How can I convert a string to a particular date format?

i.e MyString = "20111509 ".....I want to convert it to a particular format like MM/dd/yyyy or dd/MM/yyyy

Thanks.
0
Comment
Question by:theartha
[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
  • 4
  • 4
  • 4
  • +1
13 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 36543130
MyDate = Format(Ctype(MyString, Date), "MM/dd/yyyy")
This with VB-Net.
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 36543151
If your string isn't recognized as a date, you'll have to play with LEFT(), MID() and RIGHT() functions. For example, if your string is in that format, you'll have to use:

MyDate = Right(MyString, 2) & "/" & Mid(MyString, 5, 2) & "/" & Left(MyString, 4)
0
 

Author Comment

by:theartha
ID: 36543165
@Cluskitt:
I used Format(Ctype(MyString, Date), "MM/dd/yyyy") and I got the below error:

Conversion from string "20110829" to type 'Date' is not valid.

Please advice.
0
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.

 
LVL 18

Expert Comment

by:Cluskitt
ID: 36543169
In that case, I refer you to my second post :)
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36543176
dim x as string
dim d as datetime
datetime.tryparse(MyString, d)
x = d.tostring("MM/dd/yyyy")
0
 

Author Comment

by:theartha
ID: 36543216
@emoreau:

I always get the date as 01/01/0001

0
 
LVL 18

Expert Comment

by:Cluskitt
ID: 36543229
theartha: see above (http://www.experts-exchange.com/Programming/Languages/.NET/Q_27309888.html?cid=1572#a36543151). That should work fine. If necessary, you can even:
MyDate = CType(Right(MyString, 2) & "/" & Mid(MyString, 5, 2) & "/" & Left(MyString, 4), Date)
0
 

Author Comment

by:theartha
ID: 36543258
@Cluskitt:

Your solution is working fine. I just tried the other solution.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36543264
what do you have in MyString?

Can you show your code?
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36543280
you can also use:
d = Date.ParseExact(MyString, "yyyyMMdd", Nothing)

BTW, your date is invalid: 20111509
There are no valid format such as y-d-m
0
 

Author Comment

by:theartha
ID: 36543290
@Emoreau:

Dim _busDate as String = "20110829"
Dim thisDate As DateTime
DateTime.TryParse(_busDate, thisDate)
Return thisDate.ToString("MM/dd/yyyy")
0
 
LVL 40
ID: 36543300
string.Format("{0:##/##/####}",CInt(MyString))
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 36543305
Return _busDate.ToString("MM/dd/yyyy")
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…

734 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