Skip to main content
November 11, 2009
Answered

Newbie Date Conversion/Comparison Question

  • November 11, 2009
  • 3 replies
  • 524 views

I am trying to compare the date of a file with the current time using this code


<cfset myPath = "C:\path\to\myfile.txt" >
<cfset myExists=Fileexists(myPath)>
<cfSet TIMEOUT="#DateAdd("h", "-6", Now() )#"    >           
<cfset myFile = CreateObject("java", "java.io.File")>
<cfset myFile.init(myPath)>
<cfset last_modified = myFile.lastModified()>
<cfif  (TIMEOUT lt last_modified) or (not myExists)>

   if myPath does not exist or it is at least 6 hours old regenerate the file

</cfif>

My problem is the DateAdd function creates the date/time in a {ts  xxx} format while the .lastModified function creates a time stamp in a millisecond format.

What function do I use to compare these two different time stamps?

    This topic has been closed for replies.
    Correct answer Dan_Bracuk

    You might have better luck starting with cfdirectory and the datelastmodified field.  Here are some snippets for one of my programs that does date based file maintenance.

    ThisDate1 = DateAdd("d", -90, now());

    <cfdirectory name="AllFiles" directory="#Dir1#">

    <cfif AllFiles.recordcount gt 0>

    <cfquery name="OldFiles" dbtype="query">
    select name from AllFiles
    where datelastmodified < #ThisDate1#
    </cfquery>

    etc

    3 replies

    Inspiring
    November 11, 2009

    My problem is the DateAdd function creates the date/time in a {ts  xxx} format while the .lastModified function creates a time stamp in a millisecond format.

    What function do I use to compare these two different time stamps?

    Have a look at CF's date-oriented functions:

    http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6986.html

    When doing so, bear in mind your requirement (the key word of which I've highlighted).

    --

    Adam

    Inspiring
    November 11, 2009

    When doing so, bear in mind your requirement (the key word

    of which I've highlighted).

    Yes, I imagine some of the date functions probably would work with an epoch. But it seems like the long way around. Unless you really need to create a File object.

    In CF8, cfdirectory + filter would give you the file modified date. Granted as a string. But it should work with date functions (en locale).

    <cfdirectory directory="c:\pathTo" filter="someFile.txt" name="result" />

    <cfif NOT result.recordCount OR  dateDiff("h", result.dateLastModified, now()) gt 6>

    regenerate the file

    </cfif>

    For CF9, I believe you could get the modified date from the GetFileInfo() function.

    -Leigh

    Inspiring
    November 11, 2009

    What function do I use to compare these two different time

    stamps?

    As mentioned, it is probably simpler to work with the results of cfdirectory.

    But to answer your question, lastModified is an epoch date. You could

    A) Convert it to a datetime object. Then compare the two dates.

    B) Use the undocumented getTime() function, to get the milliseconds from your date object ie #now().getTime()#

    There are also a few gotchas with epochs..

    http://www.brooks-bilson.com/blogs/rob/index.cfm/2007/10/11/Some-Notes-on-Using-Epoch-Time-in-ColdFusion

    Dan_BracukCorrect answer
    Inspiring
    November 11, 2009

    You might have better luck starting with cfdirectory and the datelastmodified field.  Here are some snippets for one of my programs that does date based file maintenance.

    ThisDate1 = DateAdd("d", -90, now());

    <cfdirectory name="AllFiles" directory="#Dir1#">

    <cfif AllFiles.recordcount gt 0>

    <cfquery name="OldFiles" dbtype="query">
    select name from AllFiles
    where datelastmodified < #ThisDate1#
    </cfquery>

    etc