Click here to Skip to main content
15,919,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all i have date that is not well formed,for example '
VB
20110108

' like this .

i want to convert the above date to correct format 2011/01/08 like this. please give me sample code for this
Posted

Assuming your date is an actual date and not a String you can use the following approach to have it formatted correctly.
Custom Date and Time Format Strings[^]
When Culture dependent you can use: DateTimeFormatInfo Class[^]

When you do actually have a String and need to convert it to a Date first you can try the DateTime.TryParse[^] Method.

Hope it helps :)
 
Share this answer
 
Comments
sameertm 19-Nov-11 12:45pm    
can u give me sample code... pls your help will be greatful to me
Sander Rossel 19-Nov-11 13:22pm    
There are plenty of sample codes in the links I gave you...
For example:

Dim dt As DateTime = DateTime.Now
textBox1.Text = dt.ToString("yyyy/MM/dd") ' Shows 2011/11/19.
sameertm 19-Nov-11 13:25pm    
hi my data format is like this 20110108 how can i change it to 2011/01/08,can u give sample code for this
Sander Rossel 19-Nov-11 13:39pm    
I did... Please read the links.
It also seems your data is a String, so you should convert it to Date first.
RaviRanjanKr 20-Nov-11 10:13am    
My 5+
try this code:

VB
Dim str As String = "20110108"
Dim pattern As String = "yyyyMMdd"
Dim FindDate As Date
Dim FinalDate As String
If DateTime.TryParseExact(str, pattern, Nothing, DateTimeStyles.None, FindDate) Then
      FinalDate = FindDate.ToString("yyyy/MM/dd")
End If



www.authorcode.com
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900