Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

subtracting dates question

I have a script that I want to use to check the number of days old a file is. 2 things happen, I check to make sure the file is more then 5 days old and then I set the number of days old as a variable.

This is what I have but my problem is that it doesn't work if the file's mod date is in a different month.

How can I get this to work taking into account month and probably year?

property NumDays : 0 --Number of Days to check
property ProofsOutFolder : "my:location"


set dateNow to current date

tell application "Finder"
set theFiles to every file of folder ProofsOutFolder
repeat with aFile in theFiles
set fileModDate to modification date of aFile
if ((day of dateNow) - (day of fileModDate)) ≥ NumDays then
set DaysOut to ((day of dateNow) - (day of fileModDate))

Posted on Mar 3, 2009 11:27 AM

Reply
Question marked as Best reply

Posted on Mar 3, 2009 1:15 PM

When you subtract two dates, the result is the difference in seconds. There are built-in constants for the number of seconds in various time elements, such as hours and days, so you can subtract one date from the other and divide by the days constant (or the number of seconds in a day) to get the number of days.

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #FFEE80;
overflow: auto;"
title="this text can be pasted into the Script Editor">
property NumDays : 5

set DateNow to (current date)
set FilterTime to (NumDays * days) -- the number of seconds

set TheFIle to (choose file)
tell application "Finder"
set FileModDate to modification date of TheFIle
log "modification date: " & FileModDate
set TimeDiff to (DateNow - FileModDate) -- the difference in seconds
if TimeDiff ≥ FilterTime then
set DaysOut to (TimeDiff div days) -- the number of days
log DaysOut
end if
end tell
</pre>
2 replies
Question marked as Best reply

Mar 3, 2009 1:15 PM in response to BenChase

When you subtract two dates, the result is the difference in seconds. There are built-in constants for the number of seconds in various time elements, such as hours and days, so you can subtract one date from the other and divide by the days constant (or the number of seconds in a day) to get the number of days.

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px;
color: #000000;
background-color: #FFEE80;
overflow: auto;"
title="this text can be pasted into the Script Editor">
property NumDays : 5

set DateNow to (current date)
set FilterTime to (NumDays * days) -- the number of seconds

set TheFIle to (choose file)
tell application "Finder"
set FileModDate to modification date of TheFIle
log "modification date: " & FileModDate
set TimeDiff to (DateNow - FileModDate) -- the difference in seconds
if TimeDiff ≥ FilterTime then
set DaysOut to (TimeDiff div days) -- the number of days
log DaysOut
end if
end tell
</pre>

subtracting dates question

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.