• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 138
  • Last Modified:

Calculating dates in Visual Basic 6

Lo folks,

I need an easy way of doing this, I could write some massive bit of code so my program can understand dates and times, and months and years... but if theres an easier way I want to know!

Say I tell my program a date, say for example todays date... 2/8/2007 (or 20070802 in YYYYMMDD) - I'd like my program to tell me what the date was X amount of days earlier.

Anyknow know of any easy way of doing this is?
0
netcentraltech
Asked:
netcentraltech
1 Solution
 
Patrick MatthewsCommented:
Hello netcentraltech,

You can do date arithmetic:

DateVariable = #2 Aug 2007#
Debug.Print "Yesterday was: " & DateVariable - 1


Regards,

Patrick
0
 
DhaestCommented:
Public Function AdjustDays(FutureDate As Boolean, _
  NumberOfDays As Integer, Optional RelativeTo As Variant) _
  As Date

'***************************************
'PURPOSE: RETURNS A DATE X NUMBER OF DAYS BEFORE OR AFTER
 'A GIVEN DATE

'PARAMETERS:

'FutureDate: True if you want a date in the future
'relative to the given date, false if you want a date in the past

'NumberOfDays: How Many Days in the Future or Past you want
'to add or subtract from the given date

'RelativeTo (Optional): The given date; defaults to Today

'EXAMPLE: returns date 30 days from today

'dim dDate as date
'dDate = AdjustDays(True, 30)
'***************************************
   Dim dDate As Date
   Dim iDirection As Integer
   If IsMissing(RelativeTo) Then
       dDate = Format(Now, "Short Date")
   ElseIf Not IsDate(RelativeTo) Then
       Err.Raise 20000, , "Date required"
       Exit Function
   Else
       dDate = Format(CDate(RelativeTo), "Short Date")
   End If
   iDirection = IIf(FutureDate, 1, -1)
  AdjustDays = DateAdd("d", NumberOfDays * iDirection, dDate)
End Function

Source: http://www.freevbcode.com/ShowCode.Asp?ID=980
0
 
Patrick MatthewsCommented:
Dhaest,

Not a criticism, as I just may not be getting it--what is the advantage of using that UDF
over simple arithmetic or just using the native DateDiff() function?  I ask only because
I want to learn.

Regards,

Patrick
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
DhaestCommented:
If you put it in a function, it's more general. So you can use it from more places in your code.
For example: somewhere in the program you need to calculate 5 days before the current date end somewhere else you need to calculate 5 days further.
In that case you just have to call the function with the right params.

Off course you can also use the native datediff-function everytime..
0
 
PaulHewsCommented:
Date arithmetic as per Patrick's comment above is an option and works because VB holds dates internally as doubles, with the integer part representing days, and the fractional part representing parts of days.

The DateAdd function is a very flexible way to add any amount of time to a date/time variable.  I have a few issues with the function that Dhaest has found.  
-The unnecessary conversion from variant and to string.  
-Why add a boolean parameter, when entering a positive or negative integer should suffice
-Overly complicated for achieving a simple result.


Simply calling DateAdd with the number of days will return the value needed.

Debug.Print "Three days ago it was " & DateAdd("d", -3, Date)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
DateAdd() is definitely the way to go as it keeps the code easy to understand.

While "Date arithmetic" does work, not everyone can look at code like that and immediately understand what is happening...especially when you are not simply adding days.

Here is the documentation for DateAdd():
http://msdn2.microsoft.com/en-us/library/cb7z8yf9.aspx

The other intervals you can use are:

yyyy = Year
q = Quarter
m = Month
y = Day of year
d =  Day
w = Weekday
ww = Week of year
h = Hour
n = Minute
s = Second
(Some of the intervals are actually equivalent to each other)
0
 
Patrick MatthewsCommented:
Idle_Mind said:
>>While "Date arithmetic" does work, not everyone can look at code like that and immediately
>>understand what is happening...especially when you are not simply adding days.

No disagreement here.  If I needed to add or subtract an hour, I would not add or subtract 1/24.
I'd use DateAdd.  If it's a matter of adding days, well, I'm lazy by nature :)

Patrick
0
 
DhaestCommented:
Can you give some feedback if one of the answers was sufficient or do you still have any problems ?
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now