Solved

Date Conversion

Posted on 2001-06-15
19
130 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
 

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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 …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now