-2

I am trying to make a VBScript code which will properly guess how many days until a person's birthday. Here is the code:

days = inputbox("What is your day of birth? (Numbers please)")
months = inputbox("What is your month of birth? (Numbers please)")
years = inputbox("What is your year of birth? (Numbers please)")
leapyear = inputbox("Is " & years & " a leap year? (Yes or no)")

if not isnumeric(days & months & years) then
    msgbox("You may have entered a letter or a number from a foreign language. Latin numbers only.")
elseif isnumeric(days & months & years) then
    a = months * 30 'broken line! please fix
    if months = 1 then
        a = a + 1
    elseif months = 3 then
        a = a + 1
    elseif months = 5 then
        a = a + 1
    elseif months = 7 then
        a = a + 1
    elseif months = 8 then
        a = a + 1
    elseif months = 9 then
        a = a + 1
    end if

    if leapyear = "yes" then
        leapyear = true
    elseif leapyear = "no" then
        leapyear = false
    end if

    if months = 2 and leapyear = false then
        a = a - 2
    elseif months = 2 and leapyear = true then
        a = a - 1
    end if

    b = msgbox("You have approximately " & a & " days until your birthday.")
end if

When run, it goes about 30-60 days off. Any formula or solution?

eglease
  • 2,445
  • 11
  • 18
  • 28
  • 1
    Please at least make an effort to properly format your code when posting here. (You should also learn how to properly indent your code so that it is more readable, and easy to follow the flow of execution.) – Ken White Oct 30 '21 at 03:38
  • I keep trying to format the code using the correct marks but for some reason it just doesn't work... it goes out wrong... – Khizar Caliphate Oct 30 '21 at 03:40

1 Answers1

0

The logic is simple. If your birthday is after today, subtract today from the birthday. If your birthday is before today, add one year to the birthday and subtract today from the birthday.

Dim nextBirthday
Dim birthday
Dim today
birthday = #10/28/1982#
today = Now

nextBirthday = DateAdd("yyyy", Year(today) - Year(birthday), birthday)

If DateDiff("d", nextBirthday, today) > 0 Then
    nextBirthday = DateAdd("yyyy", 1, nextBirthday)
End If

MsgBox(DateDiff("d", today, nextBirthday))
eglease
  • 2,445
  • 11
  • 18
  • 28
  • 1
    Nice. And the key point is to use **DateDiff**. – LesFerch Oct 30 '21 at 23:16
  • Yes. Ths took me a while since you can't subtract dates in VBA like you can in .NET. – eglease Oct 30 '21 at 23:46
  • 1
    Bear in mind that VBScript and VBA while similar are not the same, for example, VBScript does not support the `As` keyword in declarations as all variables of type `Variant` and other types are cast through a sub-variant type. See [Visual Basic for Applications Features Not In VBScript](https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/scripting-articles/30593abb(v=vs.84)) – user692942 Nov 01 '21 at 15:06
  • I used the example to quickly prototype the problem. The algorithm is the key and should be the same for VBA as for VB6. – eglease Nov 01 '21 at 15:10
  • 1
    @eglease Again VB6 isn't VBScript. It also doesn't help the OP if they are only familiar with VBScript if a VBA or VB6 solution is provided. It question is clearly labelled [tag:vbscript], not [tag:vba], [tag:vb6] or [tag:vb.net]. The minute to try to reproduce this code in VBScript they will get a "Syntax Error". – user692942 Nov 02 '21 at 10:09
  • I changed the code to be VBScript so why -1? – eglease Nov 04 '21 at 01:27