Consulting

Results 1 to 6 of 6

Thread: Subtracting minutes from a date

  1. #1
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location

    Subtracting minutes from a date

    I am using a Visual Basic freeware clone (RapidQ), which does not implement DateDiff and DateAdd; where could I find a sample source to subtract MINUTES to a date?
    I need to perform calculations like this:
    2006/08/21 00:01:00 MINUS 3 minutes = 2006/08/20 23:58:00
    Taking into account day or month change due to midnight is quite complex.... I guess I need some kind of timeserial.bas file... if it exists anywhere...

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm guessing that your Date/Time is still stored as a number, so you need to subtract 3/1440 (24x60).
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    Quote Originally Posted by mdmackillop
    I'm guessing that your Date/Time is still stored as a number, so you need to subtract 3/1440 (24x60).
    Thanks, but I found that it's much easier if I convert date to separate numbers, rather than doing complex math calcs to convert date to minutes, substract minutes and converting them back to the correct date:

    [VBA]
    if min-PREALARM<0 then
    min = 60-PREALARM
    if hour=0 then
    hour = 23
    if day = 1 then
    if month>1 then
    month = month - 1
    day=MonthDays(month)
    else
    month = 12
    day = MonthDays(12)
    year = year - 1
    end if
    else
    day = day-1
    end if
    else
    hour = hour -1
    end if
    else
    min = min-PREALARM
    end if
    [/VBA]

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm not sure where the complexity comes in
    [vba]
    Sub ShowTimes()
    Dim tim1 As Date, tim2 As Date

    tim1 = Now()
    tim2 = tim1 - 3 / 1440

    MsgBox Format(tim1, "dd/mm/yy hh:mm:ss") & vbCr _
    & "less three minutes" & vbCr _
    & Format(tim2, "dd/mm/yy hh:mm:ss")
    End Sub

    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Sep 2005
    Posts
    35
    Location
    I'm NOT using VisualBasic, remember? Don't even know if Format() function exists in RapidQ!

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    The Format is just for an excel demo of how you subtract a simple fraction from one time to get an earlier time. Have you tried doing this?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •