tags:

views:

50

answers:

3

I am trying to do a fairly trivial task however my experience is in .net not in vb6. Given two strings (say "10/17/94" and "10/17/95" for this example) I want to return a string expression

X years

//Date_Due and Date_Time_Performed are strings in the mm/dd/yy format
Duration = (Year(CDate(Date_Due)) - Year(CDate(Date_Time_Performed))) & " years"

but that gives me a runtime error '13' Type Mismatch.

Any suggestions?

EDIT: None of the answers have adressed this yet. The result of the conversion must be added on to the " years" string. I need the string representation not the int.

+5  A: 

Try using datediff

Duration = CStr(DateDiff("yyyy", CDate(Date_Due), CDate(Date_Time_Performed))) & " years"
froadie
@Jay Riggs - GMTA :) - just edited...
froadie
Scott Chamberlain
@Scott - You can wrap the DateDiff call in CStr()
Jay Riggs
Try using CStr()
froadie
A: 
Duration = DateDiff("yyyy", d1, d2)

However, to avoid locale-based issues, you better manually convert your strings to dates first:

d1 = DateSerial(1900 + cint(right$(literal,2)), cint(left$(literal,2)), cint(mid$(literal,4,2)))

That is provided your dates are always mm/dd/yy. If you are using locale-dependant date formats, just use the CDate function.

GSerg
DateDiff returns a int not a string, how do I cast the result in to a string?
Scott Chamberlain
A: 
Duration = CStr(DateDiff("yyyy", Date_Time_Performed, Date_Due))

The string "yyyy" resturns the interval in years. You can also return the interval in other units.

  • yyyy - Year
  • q - Quarter
  • m - Month
  • d - Day
  • ww - Week
  • h - Hour
  • n - Minute
  • s - Second
raven
DateDiff returns a int not a string, how do I cast the result in to a string?
Scott Chamberlain
@Scott: With the CStr function. I updated my answer.
raven