Date Conversion

Can someone help me convert a date with the format 20010601 (2001 is the year, 06 the month, & 01 the day) to 06/01/2001 using visual basic?
emal1975Asked:
Who is Participating?
 
deightonConnect With a Mentor progCommented:
date(truncate(sdate/10000),truncate((sdate - truncate(sdate/10000)*10000)/100),sdate - truncate(sdate/100)*100)
0
 
PaulHewsCommented:

Private Sub Command1_Click()
    Dim strdate As String
    Dim dt As Date
    strdate = "20010601"
   
    dt = DateSerial(CInt(Left$(strdate, 4)), CInt(Mid$(strdate, 5, 2)), CInt(Mid$(strdate, 7, 2)))
    Debug.Print dt
   
End Sub
0
 
PaulHewsCommented:
If you need a formatted string from the date


Debug.Print dt
Debug.Print Format(dt, "mm/dd/yyyy")
   
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
gbarenCommented:

dim sDate1 as string
dim sDate2 as string

sDate1 = "20010601"
sDate2 = left(sDate1,4) & "/" & mid(sDate1,5,2) & "/" & right(sDate1,2)
0
 
emal1975Author Commented:
Sorry...I thought VB would work in Crystal Reports, but it doens't, only Crystal syntax and Basic syntax.

0
 
gbarenCommented:
left(sDate1,4) & "/" & mid(sDate1,5,2) & "/" & right(sDate1,2)

Is pure Basic. Should work even on an Apple II+.

0
 
emal1975Author Commented:
Sorry...I thought VB would work in Crystal Reports, but it doens't, only Crystal syntax and Basic syntax.

0
 
PaulHewsCommented:
Crystal has the mid(str, start, length) function as well as a Date(year, month, day) function.  To concatenate strings use the + sign.
0
 
PaulHewsCommented:
Don't use refresh in your browser, use the reload link on the top right of your page.
0
 
emal1975Author Commented:
gbaren - Crystal give me an error and says "The ) is missing" ...Nothing appears to be missing though.
0
 
RNPCommented:
Dim s As String
Dim d As String
   
s = "20010601"
d = Right$(s, 2) & "/" & Mid$(s, 5, 2) & "/" & Left$(s, 4)
   
Debug.Print FormatDateTime(CDate(d), vbGeneralDate)
Debug.Print FormatDateTime(CDate(d), vbLongDate)
Debug.Print FormatDateTime(CDate(d), vbShortDate)
0
 
emal1975Author Commented:
Paul - How do I concatenate them?
0
 
deightonprogCommented:
ok you've sort of told us this is Crystal, not VB.

Please tell us if 20010601 is a string or a numeric?

..

if sdate is a string

date(tonumber(sDate[1 to 4]), tonumber(sDate[5 to 6]), tonumber(sDate[7 to 8]))


perhaps if sdate is a number

date(tonumber(totext(sDate)[1 to 4]), tonumber(totext(sDate)[5 to 6]), tonumber(totext(sDate)[7 to 8]))
0
 
emal1975Author Commented:
it is a number.  It seems that Crystal is not recognizing "sDate"....I keep getting errors.
0
 
deightonprogCommented:
yes, sDate is in place of your field name - you need to try

date(tonumber(totext(sDate)[1 to 4]), tonumber(totext(sDate)[5 to 6]), tonumber(totext(sDate)[7 to 8]))

where sdate is replaced by your field name
0
 
emal1975Author Commented:
Yes! That's it.
Thanks
0
 
PaulHewsCommented:
Hey emal1975, maybe next time someone goes through that kind of effort for you, you could consider giving an "A" grade?
0
 
emal1975Author Commented:
Halfway through, I raised the compensation for the question...An "Excellent" answer would have been the first one. But I will reconsider for next time.
0
 
deightonprogCommented:
Glad to have helped!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.