Query: Difference between date/time columns

uneek78

Registered User.
Local time
Today, 08:44
Joined
Mar 26, 2009
Messages
68
I have a database that has columns that include date & time. In a similar excel spreadsheet, I have an additional column that can subtract the difference between those date/time columns and give you the days/hours/minutes left. How can I mimic this in database? I am by far not a program, so please give me something simple if possible.
 
Start by looking at the DateDiff() function

David
 
Where would I be able to locate the date difference function?
 
From 1 to 10 where do you sit in respect to Access Knowledge?

What version of Access are you using?

Are you familiar with vba?

David
 
Access 2000

I'd rate myself at a 4 or 5. I find most of my information by googling and eventually get it to work after hours of wasted time.
 
Access Help on DateDiff:

Code:
DateDiff Function
 


Returns a Variant (Long) specifying the number of time intervals between two specified dates.

Syntax

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

The DateDiff function syntax has these named arguments:

Part Description 
interval Required. String expression that is the interval of time you use to calculate the difference between date1 and date2. 
date1, date2 Required; Variant (Date). Two dates you want to use in the calculation. 
firstdayofweek Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed. 
firstweekofyear Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs. 


Settings

The interval argument has these settings:

Setting Description 
yyyy Year 
q Quarter 
m Month 
y Day of year 
d Day 
w Weekday 
ww Week 
h Hour 
n Minute 
s Second 


The firstdayofweek argument has these settings:

Constant Value Description 
vbUseSystem 0 Use the NLS API setting. 
vbSunday 1 Sunday (default) 
vbMonday 2 Monday 
vbTuesday 3 Tuesday 
vbWednesday 4 Wednesday 
vbThursday 5 Thursday 
vbFriday 6 Friday 
vbSaturday 7 Saturday 

Constant Value Description 
vbUseSystem 0 Use the NLS API setting. 
vbFirstJan1 1 Start with week in which January 1 occurs (default). 
vbFirstFourDays 2 Start with the first week that has at least four days in the new year. 
vbFirstFullWeek 3 Start with first full week of the year. 


Remarks

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.

To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.

If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols.

If date1 or date2 is a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation marks (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.

When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.

Note   For date1 and date2, if the Calendar property setting is Gregorian, the supplied date must be Gregorian. If the calendar is Hijri, the supplied date must be Hijri.
 
Per attached photo. I'm attempting to get the columns:

Time of Read Back - Time of Findings = Order to Findings

In excel this was done with a simple equation of:

=TEXT(E423-D423,"d:h:mm")

Which gives:

0:18:34 or whatever depending on the difference. Basically:

days:hours:minutes

It's so simple in excel, but I need it in access.
 

Attachments

  • query.JPG
    query.JPG
    17.2 KB · Views: 271
Access does not have this inbiult functionality, you would have to write your own function to derive this answer.

1st find the difference in days
2nd find the difference in hours less the difference in days *24
repeat for mins

There may already be a fuction that has been written on this forum, hopeflly you may get a response.

David
 
I'd rate myself at a 4 or 5. I find most of my information by googling and eventually get it to work after hours of wasted time.
I suggest you (learn to) use the access help, it has many helpfull things right there...

Like the stuff D posted above.

To find the difference between two times, simply substract the two EXACTLY like you do in Excel :)
 
Appreciate your advice. I will keep searching.
 
Format(#01/01/2009 12:30:00#-#01/02/2009 16:36:30#,"D:hh:nn") =
29:04:06

David
 
1 or 2 more stupid questions, please.

1. Where do I put it in the query? In the criteria for the field I want it to show up?

I'll plug it in now and see what I come up with
 
In your query in a new column

Countdown:Format([UpperDate]-[LowerDate],"D:hh:nn")
 
I'm close. Right now it gives me 30:00:58. For the difference between:

11/2/2009 9:58am - 11/2/2009 9:00am

It's getting the time, but the day is getting 30 days. I'm still pretty excited. You've gotten me much closer to a solution than I've gotten myself.
 
This is what I have in now:

Countdown: Format([Time of Findings]-[Exam Ordered],"d:hh:nn")

I believe I left the date portion out. I'm trying to figure out now how to implement it.
 
Which is right... becuase "day 0" is 30-dec-1899
There is no day 0 you cannot use "d:hh:nn"

You can do "hh:nn" but have to use Int(date2-date1) to find the days.
 
I love the format that it is now that DCrake has me using. It should say 0 days though. 30 - 30 = 0. You know? Like my excel spreadsheet would give me:

0:2:1 (telling me that there are 0 days, 2 hours, & 1 minute difference)

If the date/time was:

11/1/2009 10:01am - 11/1/2009 8:00am
 
Attached is a picture of what it looks like in my spreadsheet.
 

Attachments

  • spreadsheet.JPG
    spreadsheet.JPG
    45.1 KB · Views: 180

Users who are viewing this thread

Back
Top Bottom