Solved

Date Conversion

Posted on 2001-06-15
19
137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

695 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