Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Date Conversion

Posted on 2001-06-15
19
Medium Priority
?
142 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 150 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month20 days, 16 hours left to enroll

810 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