Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DayOfYear returning -1

Status
Not open for further replies.

programmher

Programmer
May 25, 2000
235
0
0
US
I am trying to get the day of the year for an enter date, the day of the year for a process date, and subtract the latter from the former to get the difference.

For some reason, DayOfYear(EnterDate) and DayOfYear(ProcessDate) always outputs -1. What am I doing wrong?
 
Hi! It seems to work for me...can you post the code?
Thanks!
 
Have you tried using the DateDiff() function instead?

#DateDiff("d", EnterDate, ProcessDate)# - tleish
 
Snix,

That WAS my code (believe it or not)! Here is how my data is formatted:

EnterDate = 20010711
ProcessDate = 20010828

My code:

<cfoutput query=&quot;MyDates&quot;>

<cfset ActDate = EnteredDate - ProcessDate>

#DayOfYear(EnterDate)#
#DayOfYear(ProcessDate)#
#ActDate#

</cfoutput>

Here is what is returned:
-1
-1
247

tleish:

When I use DateDiff, I get 117. I'm trying to find the amount of days between the two dates (like trying to find the difference between two Julian Dates).

My actual result should be 48.


 
Hi programmher:

The strings are not being interpreted as a ColdFustion date/time object, I guess. I wrote the following somewhat cheesy code (since I'm relatively new at this), which works and gets the correct answer, but I'm sure some of the real experts of this forum could come up with something much more sophisticated, but here goes:


<cfset EnterDate = 20010711>
<cfset ProcessDate = 20010828>
<cfset year = Left(EnterDate,4)>
<cfoutput>#year#<br></cfoutput>
<cfset month = Mid(EnterDate,5,2)>
<cfoutput>#month#<br></cfoutput>
<cfset day = Right(EnterDate,2)>
<cfoutput>#day#<br></cfoutput>
<!--- make the date time object --->
<cfset newdate1 = CreateDate(#year#,#month#,#day#)>

<cfset year = Left(ProcessDate,4)>
<cfoutput>#year#<br></cfoutput>
<cfset month = Mid(ProcessDate,5,2)>
<cfoutput>#month#<br></cfoutput>
<cfset day = Right(ProcessDate,2)>
<cfoutput>#day#<br></cfoutput>
<cfset newdate2 = CreateDate(#year#,#month#,#day#)>

<cfoutput>Difference = #DateDiff(&quot;D&quot;,newdate1,newdate2)#</cfoutput>

Hope this helps!
 
DateDiff() gives you the option to find the difference of Year, Quarter, Month, Day, Weekday, Week, Hour, Minute, or Second between 2 different dates.

Try this:

if
EnterDate = 20010711
ProcessDate = 20010828

<!--- Format the dates yyyymmdd to mm/dd/yyyy using Regular Expression--->
<CFSCRIPT>
reg_expression = &quot;(
Code:
[
Code:
[
:digit:
Code:
]
Code:
]
{4})(
Code:
[
Code:
[
:digit:
Code:
]
Code:
]
{2})(
Code:
[
Code:
[
:digit:
Code:
]
Code:
]
{2})&quot;;
substring = &quot;\2/\3/\1&quot;;
EnterDate = ReReplace(EnterDate, VARIABLES.reg_expression, VARIABLES.substring);
ProcessDate = ReReplace(ProcessDate, VARIABLES.reg_expression, VARIABLES.substring);
</CFSCRIPT>

<!--- Output the difference in days between these 2 date --->
<CFOUTPUT>
#DateDiff(&quot;d&quot;, EnterDate, ProcessDate)#
</CFOUTPUT> - tleish
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top