Computing minutes, days and years

P

PeterM

I have the following on a form in AC2003.
txAlarmTime is in general format "mm/dd/yyyy hh:mm:ss ampm"

Dim tmpMinuteDifference, tmpHourDifference, tmpDayDifference,
tmpYearDifference As Variant

tmpMinuteDifference = DateDiff("n", Format(Me.AP_Start_Time,
"hh:mm:ss"), Format(Me.txAlarmTime, "hh:mm:ss"))
MsgBox tmpMinuteDifference & " minutes"

tmpHourDifference = DateDiff("h", Format(Me.AP_Start_Time, "hh:mm:ss"),
Format(Me.txAlarmTime, "hh:mm:ss"))
MsgBox tmpHourDifference & " hours"

tmpDayDifference = DateDiff("d", Format(Me.AP_Start_Date, "mm/dd/yyyy"),
Format(Me.txAlarmTime, "mm/dd/yyyy"))
MsgBox tmpDayDifference & " days"

tmpYearDifference = DateDiff("yyyy", Format(Me.AP_Start_Date,
"mm/dd/yyyy"), Format(Me.txAlarmTime, "mm/dd/yyyy"))
MsgBox tmpYearDifference & " years"

if AP_State_Date = 1/6/2010 and txAlarmTime = "1/6/2010 08:45:00"
then tmpMinuteDifference = -15 and tmpHourDifference = -1 and
tmpDayDifference = 0 and tmpYearDifference = 0

why is the tmpHourDifference -1?

Likewise if AP_Start_Date = "1/7/2009" and txAlarmTime = "1/6/2010 09:00:00
am" then tmpMinuteDifference = 0 and tmpHourDifference = 0 and
tmpDayDifference = -364 and tmpYearDifference = -1

why is tmpYearDifference -1?

Thanks for any help you can offer!
 
J

John W. Vinson

I have the following on a form in AC2003.
txAlarmTime is in general format "mm/dd/yyyy hh:mm:ss ampm"

Dim tmpMinuteDifference, tmpHourDifference, tmpDayDifference,
tmpYearDifference As Variant

tmpMinuteDifference = DateDiff("n", Format(Me.AP_Start_Time,
"hh:mm:ss"), Format(Me.txAlarmTime, "hh:mm:ss"))
MsgBox tmpMinuteDifference & " minutes"

tmpHourDifference = DateDiff("h", Format(Me.AP_Start_Time, "hh:mm:ss"),
Format(Me.txAlarmTime, "hh:mm:ss"))
MsgBox tmpHourDifference & " hours"

tmpDayDifference = DateDiff("d", Format(Me.AP_Start_Date, "mm/dd/yyyy"),
Format(Me.txAlarmTime, "mm/dd/yyyy"))
MsgBox tmpDayDifference & " days"

tmpYearDifference = DateDiff("yyyy", Format(Me.AP_Start_Date,
"mm/dd/yyyy"), Format(Me.txAlarmTime, "mm/dd/yyyy"))
MsgBox tmpYearDifference & " years"

if AP_State_Date = 1/6/2010 and txAlarmTime = "1/6/2010 08:45:00"
then tmpMinuteDifference = -15 and tmpHourDifference = -1 and
tmpDayDifference = 0 and tmpYearDifference = 0

why is the tmpHourDifference -1?

Likewise if AP_Start_Date = "1/7/2009" and txAlarmTime = "1/6/2010 09:00:00
am" then tmpMinuteDifference = 0 and tmpHourDifference = 0 and
tmpDayDifference = -364 and tmpYearDifference = -1

why is tmpYearDifference -1?

Thanks for any help you can offer!

Well... I'm not sure why it's not 0, but there are two things you should be
aware of: you're feeding String values (the result of the Format() function
call) to DateDiff, whereas it expects Date/Time values; and secondly, DateDiff
calculates the number of *changes* of its granularity argument. That is,
DateDiff("yyyy", #12/31/2009 11:59:59PM#, #1/1/2010 00:00:00#) will return one
year, even though only one second has elapsed.

You might want to look at http://www.accessmvp.com/djsteele/Diff2Dates.html -
it's a more flexible replacement for DateDiff that may do exactly what you
want.
 
P

PeterM

John... I took a look at Diff2Dates and am using that. It's infinately
easier than all of the code I wrote. Thanks for pointing me in the right
direction.
 
J

John W. Vinson

John... I took a look at Diff2Dates and am using that. It's infinately
easier than all of the code I wrote. Thanks for pointing me in the right
direction.

No point in reinventing the wheel! Glad to have been of help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top