Link to home
Create AccountLog in
Avatar of GlobaLevel
GlobaLevelFlag for United States of America

asked on - subtract 2 dates

I read 2 dates from db... date_account_created and var_todays_date....

dim date_account_created  as date
dim var_todays_date as date values from client account into variables
if date_account_created - var_todays_date < 30 then

' the client is still within their 30 day trial period continue....
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of kaufmed
Swap the two dates in JamesBurger's suggestion; otherwise you'll be looking at negative values, which will always be less than 30  ; )
The question asked for date_account_created - var_todays_date, this is what I gave. Who knows what the variables really means ;-)
You can also use the datediff function of VB

Dim var_todays_date As Date = #3/4/2001# 'this allows hard coding variables as dates'
Dim date_account_created = #1/9/2001#
Dim daydiff As Long = DateDiff(DateInterval.Day, date_account_created, var_todays_date)
if daydiff < 30 then
     msg "Less than 30 days difference"
end if

Open in new window

When you have a way of doing it directly on the object, it is better than using a command from the language.

Stuff like DateDiff has been left there to ease the transition from VB6 to VB.NET.

VB.NET is an object oriented language, and the way to perform an operation in an OO language is to use the methods of the object you want to work with. It is usually more efficient (DateDiff probably calls Substract in the background), the syntax is often easier, and you end up learning stuff that will serve you should you ever need to learn another language (such as C#), because the methods of objects are the same in all languages while the commands specific to the language change from one to the other.

date_account_created.Subtract(var_todays_date).Days < 30 works in C# if you simply add a semi-colon at the end. DateDiff does not.