Sign in to follow this  
Followers 0
williamk

Subtracting Dates

7 posts in this topic

I have looked in the help file and searched the forum. I need to create a variable that is equal to todays date minus one day. So this script returns today's date:

include <Date.au3>
; Show current date/time in the pc's format
$td = MsgBox( 0, '',"The date is:" & _NowDate())

So if I ran this today $td would equal 6/27/2006. I would want it to equal 6/26/2007. I know this has got to be easy, but the only way I can think of doing it is spliting the date into an array, doing the math there, and then returning the date as a string. Is there an easier way?

Share this post


Link to post
Share on other sites



_DateAdd?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Use _DateAdd with negative value.


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

EEE Der, I hate it when the answer is right in front of you and you don't see it. Well, I've already asked one stupid question, may as well continue the streak. The script I am creating copies all pdf files with a date modified date that is one day previous to the current date. Here is what I have:

#include <Date.au3>
;Shows the filenames of all files in the current directory.
;Get the date before today
$dtbefore =  _DateAdd( 'd',-1, _NowCalcdate())
;find all the pdf files
$search = FileFindFirstFile("S:\scanned\Elizabeth\*.pdf")  
;Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf
While 1
    $file = FileFindNextFile($search) 
;msgbox (0, "file", $file)
    If @error Then ExitLoop
    $t = FileGetTime ("S:\scanned\Elizabeth\" & $file)
    If Not @error Then
    $dmyyyy = $t[1] & "/" & $t[2] & "/" & $t[0]
;msgbox (0, "date is:", $dmyyyy)
    endif
    if FileExists ("C:\Elizabeth\" & $file) AND $dmyyyy = $dtbefore Then
        $choice = msgbox (3, "Duplicate File Exists", "File " & $file & " already exists. Do you wish to overwrite it?")
        if $choice = 7 Then;No
        endif 
        if $choice = 6 Then;Yes
        filecopy ("S:\scanned\Elizabeth2\" & $file, "C:\Elizabeth\", 1)
        endif
        if $choice = 2 Then;Cancel
        Exit
        endif
    endif
    If Not FileExists ("C:\Elizabeth\" & $file) AND $dmyyyy = $dtbefore Then
    filecopy ("S:\scanned\Elizabeth\" & $file, "C:\Elizabeth\")
    endif
WEnd
;Close the search handle
FileClose($search)

The problem I have is that the FileGetTime command returns the date in the format of dd/mm/yyyy (06/06/2007), whereas the _DateAdd( 'd',-1, _NowCalcdate()) command returns the date in d/m/yyyy (6/6/2007). As the two values are compared in the line:

"if FileExists ("C:\Elizabeth\" & $file) AND $dmyyyy = $dtbefore Then"

They both need to have the exact same format. Iv'e played with the different flags on each command, but no dice yet. Ideas?

Share this post


Link to post
Share on other sites

Split the new string that is d/m/yyy and use "/" as the seperator. now you have an array or $array[0] = day, $array[1] = month and $array[2] = yyyy. then run something like this:

If $array[0] < 10 Then

$array[0] = "0" & $array[0]

EndIf

now your "d" day is a "dd" day.... did I miss read the question?

Share this post


Link to post
Share on other sites

You set the format of FileGetTime in this line

$dmyyyy = $t[1] & "/" & $t[2] & "/" & $t[0]

replace that with

$t[0] & "/" & $t[1] & "/" & $t[2]


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

Thanks guys. Both solutions work. Another way I discovered was to change the regional settings on my computer to a M/D/YYYY format and then use the _TimeDateFormat command to convert the $dtbefore variable. However Siao's way works independent of the regional settings on the computer. Splitting it into an array should also work independent of the regional settings, just adds more steps. Well, another lesson learned. Onwards.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0