Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
VB
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
Posted
Updated 9-Jan-14 0:48am
v2
Comments
Kornfeld Eliyahu Peter 9-Jan-14 6:05am    
And the error is?

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:
VB
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
 
Share this answer
 
Comments
Manfred Rudolf Bihy 9-Jan-14 6:52am    
My 5!
Apart from what you've found I already got distracted by the faulty Date literal.
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:
VB
DOB = #1/15/1950# 'The date of birth from your code
...


This link describes why the format is fixed and will not change with current locale: Date Data Type (Visual Basic)[^]

Regards,
— Manfred
 
Share this answer
 
v2
Comments
OriginalGriff 9-Jan-14 7:25am    
One of the things that trips beginners up all the damn time (and that I personally hate about VB) is it's weak typing, and automatic type conversion. Because the OP specifically declared DOB as a Date VB does an implicit Parse of the string and converts it. Nasty, nasty language! :laugh:
Manfred Rudolf Bihy 9-Jan-14 7:43am    
That sounds interesting, but what is this conversion based on? Does it depend on the locale and is done at runtime or is it done at compile time?
Whichever way this is done it should always be done in a consistent manner. That's why I'd prefer to use a literal as it is consistently defined.

Cheers!
OriginalGriff 9-Jan-14 8:01am    
Good questions! I'd assume that it was compile time, and based on development PC locale, but I haven't checked...

Stop press: I'm wrong!
It's a runtime conversion, at least in the debug compile:
.line 16,16 : 9,27 ''
IL_0001: ldstr "15/01/1950"
IL_0006: call valuetype [mscorlib]System.DateTime [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToDate(string)
IL_000b: stloc.0
Who'd a thunk it? That's seriously nasty...
Manfred Rudolf Bihy 9-Jan-14 8:05am    
Especially nasty when the program suddenly runs in a different locale.
One more reason for me not to touch VB.net, even if it were with a ten foot pole.
OriginalGriff 9-Jan-14 8:09am    
:thumbsup:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900