Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Convert String to date format

Posted on 2011-09-15
13
Medium Priority
?
290 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 2000 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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