Solved

Date Conversion

Posted on 2001-06-15
19
136 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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 trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 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