开发者

Number of years between two dates

开发者 https://www.devze.com 2023-01-13 17:57 出处:网络
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

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 no开发者_运维技巧t the int.


Try using datediff

Duration = CStr(DateDiff("yyyy", CDate(Date_Due), CDate(Date_Time_Performed))) & " years"


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消