Datetime 两个日期之间的年数

Datetime 两个日期之间的年数,datetime,vb6,Datetime,Vb6,我试图做一件相当琐碎的工作,但是我的经验是在.net中,而不是在vb6中。给定两个字符串(例如本例中的“10/17/94”和“10/17/95”),我想返回一个字符串表达式 X年 //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" 但这给了

我试图做一件相当琐碎的工作,但是我的经验是在.net中,而不是在vb6中。给定两个字符串(例如本例中的
“10/17/94”
“10/17/95”
),我想返回一个字符串表达式

X年

//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"
但这给了我一个
运行时错误'13'类型不匹配

有什么建议吗

编辑:
没有一个答案能解决这个问题。转换结果必须添加到“年”字符串中。我需要字符串表示,而不是int。

尝试使用
datediff

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

尝试使用
datediff

Duration = CStr(DateDiff("yyyy", CDate(Date_Due), CDate(Date_Time_Performed))) & " years"
但是,为了避免基于区域设置的问题,最好先手动将字符串转换为日期:

d1 = DateSerial(1900 + cint(right$(literal,2)), cint(left$(literal,2)), cint(mid$(literal,4,2)))
前提是您的日期始终为mm/dd/yy。如果您使用的是依赖于区域设置的日期格式,只需使用
CDate
函数即可

Duration = CStr(DateDiff("yyyy", Date_Time_Performed, Date_Due))
但是,为了避免基于区域设置的问题,最好先手动将字符串转换为日期:

d1 = DateSerial(1900 + cint(right$(literal,2)), cint(left$(literal,2)), cint(mid$(literal,4,2)))
前提是您的日期始终为mm/dd/yy。如果您使用的是依赖于区域设置的日期格式,只需使用
CDate
函数即可

Duration = CStr(DateDiff("yyyy", Date_Time_Performed, Date_Due))
字符串“yyy”以年为单位重新返回间隔。也可以用其他单位返回间隔

  • yyyy年
  • 季度
  • d日
  • ww-周
  • 小时
  • n分钟
字符串“yyy”以年为单位重新返回间隔。也可以用其他单位返回间隔

  • yyyy年
  • 季度
  • d日
  • ww-周
  • 小时
  • n分钟

Duration=DateDiff(“yyyy”、CDate(Date\u Due)、CDate(Date\u Time\u Performed))和“years”给出了类型不匹配的情况。如何将DateDiff的结果转换为字符串?@Scott-您可以将DateDiff调用封装在CStr()Duration=DateDiff(“yyyy”、CDate(Date\u Due)、CDate(Date\u Performed))和“years”中给出类型不匹配如何将dateDiff的结果强制转换为字符串?@Scott-可以将dateDiff调用包装为CStr()dateDiff返回int而不是字符串,如何将结果强制转换为字符串?dateDiff返回int而不是字符串,如何将结果强制转换为字符串?dateDiff返回int而不是字符串,如何将结果转换为字符串?DateDiff返回int而不是字符串,如何将结果转换为字符串?