?
Solved

Convert String to date format

Posted on 2011-09-15
13
Medium Priority
?
288 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
Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

 
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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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