此代码生成错误 [英] This code generates an error

查看:78
本文介绍了此代码生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sub Test()
    Dim DOB As Date
    Dim RetirementDate As Date

    DOB = "15/01/1950"

    RetirementDate = DateSerial(Year(DOB - 1) + 60, Month(DOB - 1) + 1, 1) - 1

    MsgBox RetirementDate

End Sub

推荐答案

错误非常明确:运营商' - '未定义类型'日期'和'整数'


这就是你要做的事情:从日期中减去一个整数 - 它应该把它视为休息一天吗?一个月?一年?一分钟?

编译器不知道你的意思:你必须具体。

所以试试这个:

The error is pretty clear: "Operator '-' is not defined for types 'Date' and 'Integer"

And that is what you are trying to do: subtract an integer from a date - should it treat that as "take a day off"? A month? A year? A minute?
The compiler doesn't know what you mean: you have to be specific.
So try this:
Sub Test()
    Dim DOB As Date
    Dim RetirementDate As Date

    DOB = "15/01/1950"

    RetirementDate = DOB.Date.AddYears(60).AddDays(-1)

    MsgBox(RetirementDate)

End Sub


除了@OriginalGriff所说:



您的日期字面错误。 VB.NET中的日期文字看起来像这样#M / d / yyyy#意思是:

M是最大2位数字,表示月份范围为[1 ... 12]。 br />
d是一个最大2位数字,表示月份的日期,范围为[1 ... 31],尽管允许的最大值实际上取决于月份。

yyyy总是表示年份的4位数。



示例:

In addition to what @OriginalGriff said:

Your date literal is wrong. A date literal in VB.NET looks like this #M/d/yyyy# meaning this:
M is a maximum 2 digit number denoting the month with a range from [1 .. 12].
d is a maximum 2 digit number denoting the day of the month with a range of [1 .. 31] albeit the maximum allowed really depends on the month.
yyyy is always 4 digits denoting the year.

Example:
DOB = #1/15/1950# 'The date of birth from your code
...





此链接描述了格式修复的原因,并且不会随当前区域设置而改变:日期数据类型(Visual Basic) [ ^ ]



问候,

- Manfred


这篇关于此代码生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆