Calculating time

SlinkRN

Well-known Member
Joined
Oct 29, 2002
Messages
724
I'm new to Access, so this question may be very basic - sorry. I have a database that has a table which is entered into by a form. I want to calculate "TimeInHospital" (in minutes) which is "TimeOfDischarge" minus "TimeOfAdmit". These times are all less than a day - usually just an hour or two. I'd like to use military time to enter admit and discharge time (which I did by using short time). I made a query and used the formula builder to build the formula to subtract admit time from discharge time. The result of the calculation is way off the way I'm doing it. Is there a special way of calculating military time? BTW, it didn't work when I used regular time either. o_O Can anyone help?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Hi,

First of all, a bit about how Access stores date values. All dates are stored as a numeric value with 0 being 30th December 1899, 1 being 31st December 1899, 2 being 1st January 1900 and so on. Time of day is stored as a fractional part of the number e.g. :-

37838 represents midnight on the 5th August 2003. 37838.5 represents midday on the 5th August 2003, etc.

When you subtract one date from another you will end up with a number which represents the number of days between the two dates. If the two dates are the same day then you will end up with a fractional number.

The easiest way to return the number of minutes between two date/time values is to use the DateDiff function (see VBA help for full description of this). Try something like this in your query:-

TimeInHospital: DateDiff("n",[TableName]![TimeOfAdmit],[TableName]![TimeOfDischarge])
 
Upvote 0
Thanks so much! That worked. Thank you for explaining why it wasn't working. It helps to know how Access does these things. I know I asked for the answer in minutes, but now looking at the result . . . Is there a way to have it come up in hours and minutes. So instead of 250 minutes, it would come up as 4:10 or 4 hrs 10 mins?

PS: What does the "n" do?
 
Upvote 0
Hi,

You can use the TimeSerial to do this. TimeSerial takes an Hour, Minute and Second argument and returns a date/time value. Incorporate the original formula into the TimeSerial and you should be on your way...

TimeInHospital: TimeSerial(DateDiff("h",[Admissions]![TimeOfAdmit],[Admissions]![TimeOfDischarge]),DateDiff("n",[Admissions]![TimeOfAdmit],[Admissions]![TimeOfDischarge]),0)

Check the VBA help on TimeSerial and DateDiff to work out how these work (and to answer your question about what 'n' does).
 
Upvote 0

Forum statistics

Threads
1,221,547
Messages
6,160,456
Members
451,647
Latest member
Tdeulkar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top