[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

Date Conversion

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
emal1975
Asked:
emal1975
  • 7
  • 5
  • 4
  • +2
1 Solution
 
PaulHewsCommented:

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
 
PaulHewsCommented:
If you need a formatted string from the date


Debug.Print dt
Debug.Print Format(dt, "mm/dd/yyyy")
   
0
 
gbarenCommented:

dim sDate1 as string
dim sDate2 as string

sDate1 = "20010601"
sDate2 = left(sDate1,4) & "/" & mid(sDate1,5,2) & "/" & right(sDate1,2)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
emal1975Author Commented:
Sorry...I thought VB would work in Crystal Reports, but it doens't, only Crystal syntax and Basic syntax.

0
 
gbarenCommented:
left(sDate1,4) & "/" & mid(sDate1,5,2) & "/" & right(sDate1,2)

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

0
 
emal1975Author Commented:
Sorry...I thought VB would work in Crystal Reports, but it doens't, only Crystal syntax and Basic syntax.

0
 
PaulHewsCommented:
Crystal has the mid(str, start, length) function as well as a Date(year, month, day) function.  To concatenate strings use the + sign.
0
 
PaulHewsCommented:
Don't use refresh in your browser, use the reload link on the top right of your page.
0
 
emal1975Author Commented:
gbaren - Crystal give me an error and says "The ) is missing" ...Nothing appears to be missing though.
0
 
RNPCommented:
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
 
emal1975Author Commented:
Paul - How do I concatenate them?
0
 
deightonCommented:
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
 
emal1975Author Commented:
it is a number.  It seems that Crystal is not recognizing "sDate"....I keep getting errors.
0
 
deightonCommented:
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
 
deightonCommented:
date(truncate(sdate/10000),truncate((sdate - truncate(sdate/10000)*10000)/100),sdate - truncate(sdate/100)*100)
0
 
emal1975Author Commented:
Yes! That's it.
Thanks
0
 
PaulHewsCommented:
Hey emal1975, maybe next time someone goes through that kind of effort for you, you could consider giving an "A" grade?
0
 
emal1975Author Commented:
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
 
deightonCommented:
Glad to have helped!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now