Compare if dates are in the period

Asked

Viewed 25 times

0

I need to know from the user’s birth date, if he was born in the period of the military dictatorship.

The military period in Brazil began on 04/01/1964 and ended on 03/15/1985, and all who were born between this period would be treated as true, the before and after the period would be treated as false.

I’m trying this way, but I’m missing something

Function ditadura(data)

d = day(data)
m = month(data)
a = year(data)

if a >= 1964 and m >= 04 and d >= 01 or a <= 1985 and m <= 03 and d <= 15 then 
    ditadura = "Você nasceu em um período de ditadura militar"
end if

end function

How can I fix this?

1 answer

2


You can compare date to date directly:

Function ditadura(data)

   inicio = CDate("01/04/1964")
   fim= CDate("15/03/1985")
   If data >= inicio And data <= fim Then
      ditadura = "Você nasceu em um período de ditadura militar"
   Else
      ditadura = ""
   End If

end function

Just note the server date format to set the start and end dates, you may need to use the M/D/A format

  • 1

    The code is good, but there’s a mistake: it should be If data>= inicio And data <= fim Then

  • yes truth well aimed @dvd, I will fix

  • Thanks friend, but it did not work, I checked the format and this correct, but when I type the date 17/10/1983 that was in the period, it is not informed that I was born in the dictatorship, not going, tested exactly as you posted and did not go, where can I be missing? you took the test? it was all ok?

  • Are you sure the date shape is correct?

  • I believe the problem is the format d/m/a, tried with m/d/a? I made a fiddle, a little different because it is in Vb.Net, but the logic is the same, and I did several tests and worked perfectly, including with 17/10/1983. I had to use the date format M/D/A, see here: https://dotnetfiddle.net/1NaBhL

  • 1

    Funfou mano, after testing here, I checked that the date of birth has to be in the format d/m/a the others are in the format d/m/a I will treat here and fix it, thank you very much for the help!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.