473,468 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Counting the number of days between 2 dates starting from 1 not 0

21 New Member
Hi there,

I have created Job table where I have to enter the startdate and finish date for each job I work on. The problem is that some jobs only last 1 day. I have a field called JobLength where I am trying to calculate the numerical difference between the startdate and the enddate, but it always comes back with 0. If I work on a job starting on 17/11/06 and finishing on 17/11/06, how do I get it to say 1 (as in 1 day?).

I have tried DateDiff, when using the form:

Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",[JobStartDate],[JobFinishDate])
But it still comes up with one. Any ideas?

I am using MS Access 2003.
Nov 20 '06 #1
9 3051
Killer42
8,435 Recognized Expert Expert
Hi there,

I have created Job table where I have to enter the startdate and finish date for each job I work on. The problem is that some jobs only last 1 day. I have a field called JobLength where I am trying to calculate the numerical difference between the startdate and the enddate, but it always comes back with 0. If I work on a job starting on 17/11/06 and finishing on 17/11/06, how do I get it to say 1 (as in 1 day?).

I have tried DateDiff, when using the form:

Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",[JobStartDate],[JobFinishDate])
But it still comes up with one. Any ideas?

I am using MS Access 2003.
Two possibilities come to mind.
  • Use IIF() to return one if they match, otherwise DateDiff.
  • =DateDiff("d",[JobStartDate],[JobFinishDate])-([JobStartDate]=[JobFinishDate])
Note, True generally returns -1 which is why we subtract rather than add.

I suppose the problem with either of these approaches is, are the values the same or not? If this is a date only, they would be. If it includes time, they wouldn't.
Nov 20 '06 #2
Killer42
8,435 Recognized Expert Expert
I suppose the problem with either of these approaches is, are the values the same or not? If this is a date only, they would be. If it includes time, they wouldn't.
On further reflection, it might be messy but you could probably overcome this problem (if it exists) by checking whether the returned value from DateDiff was zero, rather than whether the dates are the same. In other words
Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",[JobStartDate],[JobFinishDate])
  2.   - (DateDiff("d",[JobStartDate],[JobFinishDate]) = 0)
Another possibility would be to add, say, 0.9 to the result then use the Int function to chop off the decimal places.
Expand|Select|Wrap|Line Numbers
  1. =Int(DateDiff("d",[JobStartDate],[JobFinishDate]) + 0.9)
I don't know whether any of these are any good, but they might give you some ideas to play with.
Nov 20 '06 #3
Killer42
8,435 Recognized Expert Expert
Ok, scratch the Int idea - it won't work. I was thinking of DateDiff returning a decimal part of a day, or something.

Sorry about this folks, just brainstorming with myself, I guess.

Oh! And bumping up my message count, naturally. After all, that's what we're here for. :)
Nov 20 '06 #4
MMcCarthy
14,534 Recognized Expert Moderator MVP
I think your problem is more basic than that. If you start a job on 17/11/06 and finish on 18/11/06, I assume you want to return 2 days but this will only return 1.

Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d", #17/11/2006#, #18/11/2006#)
This should solve your problem:

Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",[JobStartDate]-1,[JobFinishDate])
Nov 20 '06 #5
Killer42
8,435 Recognized Expert Expert
I think your problem is more basic than that. If you start a job on 17/11/06 and finish on 18/11/06, I assume you want to return 2 days but this will only return 1.
Good point! You could just add 1 to the result, in that case.
Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",[JobStartDate],[JobFinishDate])+1
Not much difference either way I suppose, Mary.
Nov 20 '06 #6
antheana
21 New Member
I think your problem is more basic than that. If you start a job on 17/11/06 and finish on 18/11/06, I assume you want to return 2 days but this will only return 1.

Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d", #17/11/2006#, #18/11/2006#)
This should solve your problem:

Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",[JobStartDate]-1,[JobFinishDate])

Complete and utter genius!!!

Thank you all for your help - problem solved. I added -1 to JobStartDate and with the dates (jobstartdate) 17/11/2006 and (jobfinishdate) 17/11/06 the joblength came back as 1 not 0.

So thanks again. It is much appreciated :)
Nov 20 '06 #7
MMcCarthy
14,534 Recognized Expert Moderator MVP
Good point! You could just add 1 to the result, in that case.
Expand|Select|Wrap|Line Numbers
  1. =DateDiff("d",[JobStartDate],[JobFinishDate])+1
Not much difference either way I suppose, Mary.
Either way it works, the problem is DateDiff() doesn't include the first date in it's count.

Mary
Nov 20 '06 #8
Killer42
8,435 Recognized Expert Expert
Either way it works, the problem is DateDiff() doesn't include the first date in it's count.
Mary
Which is only as it should be, of course. I mean, you could hardly claim that the difference between a date and the same date is a day.
Nov 20 '06 #9
MMcCarthy
14,534 Recognized Expert Moderator MVP
Which is only as it should be, of course. I mean, you could hardly claim that the difference between a date and the same date is a day.
True!

Mary
Nov 20 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tiernan | last post by:
Hi all I'm looking for a way to find the number of weekdays between 2 dates In my form I have three fields for a begin date (dd)(mm)(yyyy) and three for the end date (dd)(mm)(yyyy) Now these...
7
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19...
6
by: edwardfredriks | last post by:
I'm looking for a script that, instead of counting down, can "count up" from a given date. So the output should be something like "(xx) days since (date/event)" or "(date/event) was (xx) days ago"....
2
by: David Mitchell | last post by:
I have tried using the following code to count the specific number of each weekday but get a compile error "User defined type not defined" which I think relates to the first line of the function: -...
29
by: james | last post by:
I have a problem that at first glance seems not that hard to figure out. But, so far, the answer has escaped me. I have an old database file that has the date(s) stored in it as number of days. An...
14
by: jpr | last post by:
Friends, I have a form with four fields, date1, date2, date3 and date4. All these have all a mm/dd/yyyy format and have their source to a table. I need to add an unbound control (I will name...
2
by: Jannick | last post by:
Help..please! I'm a rookie in terms of VB, but I'm desperately trying to make Access count the number of days between two dates and then subtract the weekends. Can anyone tell me what is wrong...
3
by: Tim Chase | last post by:
I've been trying to come up with a good algorithm for determining the starting and ending dates given the week number (as defined by the strftime("%W") function). My preference would be for a...
2
by: vspsdca | last post by:
Hi, I'm quite new to the world of VBA so any help you could extend will help me a lot with my project. I have a database of Change Requests from our users. I have two date boxes, one is Date...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.