Solved

How to figure out time since certain date...

Posted on 1998-08-08
15
246 Views
Last Modified: 2010-04-30
I'm just writing a small little program for fun and I was wondering how I could find the amount of time passed since a certain date.  For example, the date I want to figure out for is march 24th 1998 1:27am.  I'd like to display the years, months, days, hours, and minutes since that time :)  It's really just a fun thing I'm doing but any help would be appreciated.
0
Comment
Question by:treyjeff
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 2

Expert Comment

by:percosolator
ID: 1468320
Be glad to help you, but I have a question:

The days, months, years since when?  What date are you comparing it to?

One that the user enters?  1/1/0?  7/4/1776?
0
 
LVL 3

Expert Comment

by:MorFF
ID: 1468321
Use the DateDiff function.

eg   vDiffInMins = DateDiff("m", Now, vYourDate)

you then need to a bit of calculating to split it up.  If you need help with that bit, post me back and I'll provide that too.

Cheers - MorFF
0
 
LVL 1

Author Comment

by:treyjeff
ID: 1468322
Yes I'll need help for sure :)  What should I declare vDiffInMins as?  I have never used the datediff function and am unsure of the parameters.  Like what the m is.  The date I wanted to use is the one above.
vDiffInMins = DateDiff("m", Now, vYourDate)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:MorFF
ID: 1468323
Check out the VB help for the full details on the function.

The function returns a variant.
The "m" is to get the difference back in minutes.  You can substitute this with "s" (seconds), "d" (days) etc.

If you get the difference back then you need to divide the value by 60 and the remainder will be the number of minutes.  You then need to dvide the answer by 24 and the remainder will be the number of hours.  Then you need to get a bit cleverer and work back from the date in the month now to the beginning of the month and then back in full months to the date in question.  You can do this with a CASE statement and a loop, this will give you a total number of months and a remainder of days (remember about leap years!).  Then divide your number of months by 12 to get the number of years and the remainder of months.

You can short-cut this a bit, but it depends on how accurate you want to get.
0
 
LVL 1

Author Comment

by:treyjeff
ID: 1468324
This is harder than I thought!  I'll increase the points for some code :)
0
 
LVL 3

Expert Comment

by:MorFF
ID: 1468325
Go for it dude.
Specify exactly wot u need.
you new to VB or programming in general?  What Level do we need to go to?

MorFF
0
 
LVL 1

Author Comment

by:treyjeff
ID: 1468326
Ok, if you can give me the exact code.  It goes like this, I have a small form and label one says "You have been here for..." there is a small label that I want to hold the info in.  The preset date is March 24th 1998 and the preet time is 1:27am.  Need anymore info?
0
 
LVL 1

Author Comment

by:treyjeff
ID: 1468327
Are you going to try it for me? :)  Do you need more points?  I imagine it's not all that much code so hopefully you can help :)
0
 
LVL 3

Expert Comment

by:MorFF
ID: 1468328
I'm using a machine just now that has no VB.  I'll hopefully get it installed again within the next coupla days, then I'll see about doing some code.

Cheers - MorFF
0
 
LVL 2

Accepted Solution

by:
swilt earned 150 total points
ID: 1468329
   Dim dtePast     As Variant
    Dim nTotSecs    As Long
    Dim nDays       As Integer
    Dim nHours      As Integer
    Dim nMins       As Integer
    Dim nSecs       As Integer
   
    dtePast = "24 march 1998 1:27am"
    dtePast = "11 august 1998 17:50am"
    nTotSecs = DateDiff("s", dtePast, Now)
    nDays = nTotSecs \ 86400
    nTotSecs = nTotSecs Mod 86400
    nHours = nTotSecs \ 3600
    nTotSecs = nTotSecs Mod 3600
    nMins = nTotSecs \ 60
    nSecs = nTotSecs Mod 60
   
    MsgBox nDays & " Days " & nHours & " Hours" & vbCr & nMins & " Mins " & nSecs & " Secs"

0
 
LVL 1

Author Comment

by:treyjeff
ID: 1468330
Thanks that's great, can months and years be added?  I will increase points even more if need be.
0
 
LVL 2

Expert Comment

by:swilt
ID: 1468331
   Dim dtePast     As Variant
    Dim nTotSecs    As Long
    Dim nYears      As Integer
    Dim nMonths     As Integer
    Dim nDays       As Integer
    Dim nHours      As Integer
    Dim nMins       As Integer
    Dim nSecs       As Integer
     
    dtePast = "24 march 1996 1:27am"
   
    nYears = DateDiff("yyyy", dtePast, Date)
    dtePast = DateAdd("yyyy", nYears, dtePast)
    nMonths = DateDiff("m", dtePast, Now)
    dtePast = DateAdd("m", nMonths, dtePast)
   
    nTotSecs = DateDiff("s", dtePast, Now)
    If nTotSecs < 0 Then
        dtePast = DateAdd("m", -1, dtePast)
        nTotSecs = DateDiff("s", dtePast, Now)
        nMonths = nMonths - 1
        If nMonths < 0 Then nYears = nYears - 1
    End If
       
    nDays = nTotSecs \ 86400
    nTotSecs = nTotSecs Mod 86400
    nHours = nTotSecs \ 3600
    nTotSecs = nTotSecs Mod 3600
    nMins = nTotSecs \ 60
    nSecs = nTotSecs Mod 60
     
    MsgBox nYears & " Years " & nMonths & " Months" & vbCr & _
           nDays & " Days " & nHours & " Hours" & vbCr & _
           nMins & " Mins " & nSecs & " Secs"

0
 
LVL 2

Expert Comment

by:swilt
ID: 1468332
er..bit of an obvious mistake, I forgot to add 12 to the months when taking a year away    

    nTotSecs = DateDiff("s", dtePast, Now)
    If nTotSecs < 0 Then
        dtePast = DateAdd("m", -1, dtePast)
        nTotSecs = DateDiff("s", dtePast, Now)
        nMonths = nMonths - 1
        If nMonths < 0 Then
            nYears = nYears - 1
            nMonths = nMonths + 12
        End If
    End If
0
 
LVL 3

Expert Comment

by:MorFF
ID: 1468333
ah well, looks like you got some coding activity.

MorFF
0
 
LVL 1

Author Comment

by:treyjeff
ID: 1468334
Thank you so very much!!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading the Contents of a Directory In Access VBA 5 70
How does CurrentUser work? 10 37
Recommendation vb6 to vb.net or others 14 150
Sub or Function is not defined 6 30
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question