Solved

Convert String to date format

Posted on 2011-09-15
13
284 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

821 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