Solved

Convert String to date format

Posted on 2011-09-15
13
280 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
 
LVL 18

Expert Comment

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

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 69

Expert Comment

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

Can you show your code?
0
 
LVL 69

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 69

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now