Solved

Date Conversion

Posted on 2001-06-15
19
135 Views
Last Modified: 2010-05-02
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?
0
Comment
Question by:emal1975
  • 7
  • 5
  • 4
  • +2
19 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 6195586

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
 
LVL 38

Expert Comment

by:PaulHews
ID: 6195593
If you need a formatted string from the date


Debug.Print dt
Debug.Print Format(dt, "mm/dd/yyyy")
   
0
 
LVL 5

Expert Comment

by:gbaren
ID: 6195596

dim sDate1 as string
dim sDate2 as string

sDate1 = "20010601"
sDate2 = left(sDate1,4) & "/" & mid(sDate1,5,2) & "/" & right(sDate1,2)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

Author Comment

by:emal1975
ID: 6195636
Sorry...I thought VB would work in Crystal Reports, but it doens't, only Crystal syntax and Basic syntax.

0
 
LVL 5

Expert Comment

by:gbaren
ID: 6195661
left(sDate1,4) & "/" & mid(sDate1,5,2) & "/" & right(sDate1,2)

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

0
 

Author Comment

by:emal1975
ID: 6195674
Sorry...I thought VB would work in Crystal Reports, but it doens't, only Crystal syntax and Basic syntax.

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6195687
Crystal has the mid(str, start, length) function as well as a Date(year, month, day) function.  To concatenate strings use the + sign.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6195691
Don't use refresh in your browser, use the reload link on the top right of your page.
0
 

Author Comment

by:emal1975
ID: 6195692
gbaren - Crystal give me an error and says "The ) is missing" ...Nothing appears to be missing though.
0
 

Expert Comment

by:RNP
ID: 6195694
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
 

Author Comment

by:emal1975
ID: 6195703
Paul - How do I concatenate them?
0
 
LVL 18

Expert Comment

by:deighton
ID: 6195728
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
 

Author Comment

by:emal1975
ID: 6195734
it is a number.  It seems that Crystal is not recognizing "sDate"....I keep getting errors.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6195774
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
 
LVL 18

Accepted Solution

by:
deighton earned 50 total points
ID: 6195803
date(truncate(sdate/10000),truncate((sdate - truncate(sdate/10000)*10000)/100),sdate - truncate(sdate/100)*100)
0
 

Author Comment

by:emal1975
ID: 6195821
Yes! That's it.
Thanks
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6195846
Hey emal1975, maybe next time someone goes through that kind of effort for you, you could consider giving an "A" grade?
0
 

Author Comment

by:emal1975
ID: 6195854
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
 
LVL 18

Expert Comment

by:deighton
ID: 6195893
Glad to have helped!
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

Title # Comments Views Activity
VBA error replacing data 6 40
Exit a vb6 apps when a calling it apps closes 15 52
Add and format columns in vb6 7 63
Set WorkSheet  not Working 9 39
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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