473,375 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,375 software developers and data experts.

Count the no of days between 2 dates

139 100+
So, how do I count the number of days between two dates?

I have set my variables to carry dates:

Expand|Select|Wrap|Line Numbers
  1. <cfset dtFrom = #CreateODBCDate(dateFormat(form.Pump_FromDate, 'dd/mm/yyyy'))# />
  2. <cfset dtTo = #CreateODBCDate(dateFormat(form.Pump_ToDate, 'dd/mm/yyyy'))# />
  3. <cfset dtDiff = (dtTo - dtFrom) />
But can't output the number of days between them as a whole number.
Can anyone help?

May 18 '09 #1

✓ answered by acoder

You don't need to convert it to ODBC format. It's fine to use the date field from the database, WORecDate and QuotePDate in dateDiff. Line 14 is not required either.

9 10925
16,027 Expert Mod 8TB
Use the dateDiff() function.
May 18 '09 #2
139 100+

Amazingly I did work this one out myself, BUT, now my client has moved the goalposts...

I have produced a report that pulls info from a database - and two dates. He now wants to calculate the dateDiff between these two dates, for every record in the database (these have to be working days) and then divide the result by a number (actually the number of pumps in the queryset, which I already have due to a Recordcount)

I can create the following for on date diff:
Expand|Select|Wrap|Line Numbers
  1. <cfset dtFrom = #CreateODBCDate(dateFormat(form.Pump_FromDate, 'dd/mm/yyyy'))# />
  2. <cfset dtTo = #CreateODBCDate(dateFormat(form.Pump_ToDate, 'dd/mm/yyyy'))# />
  3. <cfset dtDiff = (dtTo - dtFrom) />
  4. <cfset ctDays = DateDiff ("d", dtFrom, dtTo)>
Can I:
- use this with the fields from my database that hold the two dates
- loop over the query to retrieve all the days as a total
- and ensure that it only counts working dates?

It's a big ask so any help would be appreciated!
May 19 '09 #3
139 100+
Oh, and loop over the results where the second date is NULL!
May 19 '09 #4
139 100+
Ah, another spanner...

I've created a query to loop through the recordset and return the dateDiff in ODBC Date format:

Expand|Select|Wrap|Line Numbers
  1.     <table>
  2.     <tr>
  3.     <th>WONO</th>
  4.     <th>Client</th>
  5.     <th>PowerRating</th>
  6.     <th>Logged Date</th>
  7.     <th>Quote P Date</th>
  8.     <th>No Of Days</th>
  9.     </tr>
  11.     <cfloop query="getPumpsDate">
  12.     <cfset dtFrom = #CreateODBCDate(dateFormat(WORecdate, 'dd/mm/yyyy'))# />
  13.     <cfset dtTo = #CreateODBCDate(dateFormat(QuotePDate, 'dd/mm/yyyy'))# />
  14.     <cfset dtDiff = (dtTo - dtFrom) />
  15.     <cfset ctDays = DateDiff ("d", dtFrom, dtTo)>
  16.     <cfoutput>
  17.     <tr>
  18.     <td>#ClientWONO#</td>
  19.     <td>#ClientFK#</td>
  20.     <td>#PowerRating#</td>
  21.     <td>#dateFormat(WORecdate, "dd/mm/yyyy")#</td>
  22.     <td>#dateFormat(QuotePDate, "dd/mm/yyyy")#</td>
  23.     <td>#ctDays#</td>
  24.     </tr>
  25.     </cfoutput>
  26.     </cfloop>
  27.     </table>
However the results are returning negative values in some instances - any idea why?

Logged Date Quote P Date No Of Days
06/04/2009 21/04/2009 -44
06/04/2009 22/04/2009 -43
16/04/2009 21/04/2009 5
24/04/2009 30/04/2009 6

I assume its seeing the month and day back to front, but don't know why...
May 19 '09 #5
16,027 Expert Mod 8TB
You don't need to convert it to ODBC format. It's fine to use the date field from the database, WORecDate and QuotePDate in dateDiff. Line 14 is not required either.
May 19 '09 #6
139 100+
Thanks acoder - it worked perfectly.

Now that i have created my loop and worked out all the days, do you know how I can add them all together? Plus is there any function that will calculate these in business days?
May 19 '09 #7
16,027 Expert Mod 8TB
Keep a running count variable for the total and add ctDays on each round of the loop.

For business days, look at the DateAdd function with the "w" parameter.
May 19 '09 #8
139 100+
hi Acoder,

I've added the DateAdd function but it doesn't seem to work:

Expand|Select|Wrap|Line Numbers
  1. <cfset intDayTotal = 0>
  2.         <cfloop query="KWBand1C">
  4.         <cfset dtFrom = #DateAdd('w', -2, WORecdate)# />
  5.         <cfset dtTo = #DateAdd('w', -2, QuotePDate)# />
  6.         <cfset ctDays = DateDiff ("d", dtFrom, dtTo)>
  7.         <cfset intDayTotal = intDayTotal + ctDays>
  8.         <cfoutput>
Seemed a bit too easy to me - am I doing it wrong?
May 20 '09 #9
16,027 Expert Mod 8TB
Yes, that's a bit too simple. You need to use a while loop and date add one by one until it reaches the second date, e.g.
Expand|Select|Wrap|Line Numbers
  1. <cfscript>
  2. function workingDaysBetween(date1,date2) {
  3.     var numDays = 0;
  4.     while (date1 LT date2) {
  5.         date1 = dateAdd("w",1,date1);
  6.         numDays = numDays + 1;
  7.     }
  8.     return numDays;
  9. }
  10. </cfscript>
(not tested)
May 20 '09 #10

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

Similar topics

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...
by: BlackFireNova | last post by:
I need to write a report in which one part shows a count of how many total records fall within the working days (Monday - Friday) inside of a (prompted) given date range, in a particular...
by: jpr | last post by:
Is there a way I can count days? I would like to place a control on my form that counts the days from the first day of each year. THanks.
by: newnewbie | last post by:
Was looking all over to find an answer to my question, but did not. I have a database that lists different users entering multiple records every day. For me, the only way to see how many days the...
by: DeanO | last post by:
I want to count the number of days reference a shipping order. I need to exclude weekends and holidays. Thank you.
by: ghjk | last post by:
How can i compare two days in php and my sql? In my web application user should register a date and the validity period(ex.5 days). I want to count days and cancel the user operations after that...
by: Mike | last post by:
I have a routine that's calculating business days but its not counting the weekend days that are between the start date and end date. If my start date is 9/26/08 and my end date is 10/01/08, I...
by: sha2484 | last post by:
I need help to count days between 2 dates,where i want to count the days between current date and registeration date.Here is the code that i have write but it only compare the days , it do not...
by: Scholar81 | last post by:
Hello, I am a novice Access developer and my boss asked me to build a database and I said yes. Now I realized the bite is way too big and I'm trying not to choke ;-) And what's worse, he does...
Jerry Maiapu
by: Jerry Maiapu | last post by:
Hi Scott Prince, Since this dean is not interested I need to do the same as Dean is trying to do. Since Dean has not replied would you mind to show me how to do this: You'll have to open this into...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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...

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.