Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

Time - elapsed time between 2 dates


'elapsed Time between dates (D2 - D1)
'subtracting 2 dates give a Single which Is the number of days
Dim D1 As Date
Dim D2 As Date
DDays = D2 - D1

'calculate all other values As a Function of DDays
DYears = DDays / 365
DMonths = DDays / 365 / 12
DHours = DDays * 24
DMinutes = DDays * 24 * 60
DSeconds = DDays * 24 * 60 * 60

'==========================================
'alternate approach
'get difference between to Dates, in an internval of your choice
Dim D1 As Date
Dim D2 As Date
Dim Interval As String
Interval =  yyyy |  q  |  m  |  y  |  d  |  w  |  ww  |  h  |  n  |  s
Year |  Quarter  |  Month  |  Day of year  |  Day  |  Weekday  |  Week  |  Hour  |  Minute  |  Second
Dim Result
Result = DateDiff( "d" , D1, D2)  'days between the dates
Result = DateDiff( "w" , D1, D2)  'weekdays between the dates
Result = DateDiff( "h" , D1, D2)  'hours between the dates

'the VB function 'DatePart' lets you extract just a part of the date
'the VB function 'DateAdd' lets you add an interval(s) to a date