Sign in to follow this  
Followers 0
disney

Date comparison

8 posts in this topic

My problem is this:

I want to copy a date from a program and then compare it with the current date. The goal is to check if the different is more then 20 work days (except sat/sun)

i have a textstring in the clipboard, for exemple 2008-04-28

is want to compare it to the current date 2008-05-07

and i want to compare it (i don't want to count saturdays and sundays)

This is all i've done for now:

$calldate = ClipGet()

MsgBox(0, "Clipboard contains:", $calldate)

#include <Date.au3>

MsgBox( 4096, "Pc Short format", _DateTimeFormat( _NowCalc(),2))

if $calldate < ..

Then I dont know. Can somebody help me to get "_DateTimeFormat( _NowCalc(),2))" to a variable and then compare it with $calldate and see if the different is 20 days or more between them (do not count sat/sundays). The result should be present in a msgbox or variable true/false.

Please help.

Thanks from Sweden / Hans

Share this post


Link to post
Share on other sites



Try this.

#include <Date.au3>

$calldate = ClipGet()
$CurrentDate = _NowCalcDate()
$sDate = StringReplace($calldate,"-","/")

$TotalDaysDifference = _DateDiff("d",$CurrentDate,$sDate)

$iTemp = Abs($TotalDaysDifference)

For $x = 1 to $iTemp
    ConsoleWrite($sDate & @CRLF)
    $aTemp = StringSplit($sDate,"/")
    
    If _DateToDayOfWeekISO($aTemp[1],$aTemp[2],$aTemp[3]) > 4 Then $iTemp -= 1
    
    If $TotalDaysDifference < 0 Then
        $sDate = _DateAdd("d",1,$sDate)
    Else
        $sDate = _DateAdd("d",-1,$sDate)
    EndIf
    
Next
    
msgbox(1,"","Total Working Days difference: " & $iTemp)

Share this post


Link to post
Share on other sites

Hi.

I want to copy a date from a program and then compare it with the current date.

Straight from the help file :)

#include <Date.au3>

; Calculated the number of seconds since EPOCH (1970/01/01 00:00:00) 
$iDateCalc = _DateDiff( 's',"1970/01/01 00:00:00",_NowCalc())
MsgBox( 4096, "", "Number of seconds since EPOCH: " & $iDateCalc )

; Calculated the number of Hours this year 
$iDateCalc = _DateDiff( 'h',@YEAR & "/01/01 00:00:00",_NowCalc())
MsgBox( 4096, "", "Number of Hours this year: " & $iDateCalc )

_DateDayOfWeek() will help to calculate weekends.

Keep in mind to have a list of fixed holidays and variable holidays (Easter, ...)

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

#include <Date.au3>
$date1 = "300408"                       ; 300508  -  30 April 2008
                                        ; 1 Sat and 1 Sun between  -  inclu. 8 days excl. 6 days
$date2 = @MDAY & @MON & StringRight(@YEAR,2); 070508  -  7 May 2008 (Today)

$time = StringMid($date2,3,2) - StringMid($date1,3,2)

If $time = 1 Then 
    $dayn = _calcdate(StringLeft($date1,2),StringMid($date1,3,2))
    $totaldays = $dayn + StringLeft($date2,2)
EndIf

If $totaldays > 7  And $totaldays < 13 Then
    $days = $totaldays - 2
    ConsoleWrite("Total days: " & $days&@LF)
EndIf

Func _calcdate($day,$mon)
    $new = _DateDaysInMonth(@year,$mon)-$day+1
    Return $new
EndFunc

I think this will help too

Edited by UQOII

[center]uqoii.nl[/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

#include <Date.au3>
 
 ConsoleWrite(_countWorkdays("2008/05/07", "2008/05/12") & @CRLF)
 
 Func _countWorkdays($startDate, $endDate)
     Local $days = _DateDiff('d', $startDate & ' 00:00:00', $endDate & ' 00:00:00')
     Local $weekday
     For $i = 0 To $days
         $weekday = _DateToDayOfWeek(StringLeft($startDate, 4), StringMid($startDate, 6, 2), StringMid($startDate, 9, 2))
         If $weekday = 1 Or $weekday = 7 Then $days -= 1
         $startDate = _DateAdd('d', 1, $startDate & ' 00:00:00')
     Next
     Return $days
 EndFunc  ;==>_countWorkdays

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Thanks for all of your answers, i will test and see witch one of the solutions that works best in my script. I will sure have more questions later.

Again Thanks!

/Hans

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

What do you think? Still holidays and so on aren't in the script, but i'm pretty close. Don't know how to fix all holidays.

(The text I copy from lotus is "Open 2008-05-01 by Hans Zetterberg/DEP1/COMP")

I Will paste the whole script and comment so you understand better.

Opt("WinTitleMatchMode",2)

WinActivate("Lotus") ; Active the correct window

WinWait("Lotus") ; Wait for the window

MouseClick("left", 950,155) ; Marking the text to copy

MouseClick("right") ; Bring the menu

Send("{DOWN 3}") ; Choose Copy

Send("{ENTER}") ; Enter

$calldate = ClipGet() ;Get the text from the clipboard to a variable called calldate

MsgBox(0, "Clipboard contains:", $calldate) ; Just for testing purpose

$calldate2 = StringMid($calldate,8,10) ; Trim the text and create a new string with the trimmed string in, i call it calldate2

MsgBox(0, "String middad characters is:", $calldate2) ; Just for testing purpose

$calldate = $calldate2

$CurrentDate = _NowCalcDate()

$sDate = StringReplace($calldate,"-","/")

$TotalDaysDifference = _DateDiff("d",$CurrentDate,$sDate)

$iTemp = Abs($TotalDaysDifference)

For $x = 1 to $iTemp

ConsoleWrite($sDate & @CRLF)

$aTemp = StringSplit($sDate,"/")

If _DateToDayOfWeekISO($aTemp[1],$aTemp[2],$aTemp[3]) > 4 Then $iTemp -= 1

If $TotalDaysDifference < 0 Then

$sDate = _DateAdd("d",1,$sDate)

Else

$sDate = _DateAdd("d",-1,$sDate)

EndIf

Next

msgbox(1,"","Total Working Days difference: " & $iTemp)

---

Can something be re-done better?

/Hans

Edited by disney

Share this post


Link to post
Share on other sites

I've modified it to read from a holidays file. I have a script at work sort of like this, and I found the best way to deal with holidays was to have a seperate file that you could edit, rather than hard coding in all the holidays for the next whatever years. That way, if schedules change around the holidays (eg: christmas) you can just modify your holidays file without having to edit and recompile your script.

#include <Date.au3>
#include <File.au3>

Opt("WinTitleMatchMode",2)

Global $aHolidays

WinActivate("Lotus") ; Active the correct window
WinWait("Lotus") ; Wait for the window
MouseClick("left", 950,155) ; Marking the text to copy
MouseClick("right") ; Bring the menu
Send("{DOWN 3}") ; Choose Copy
Send("{ENTER}") ; Enter

$calldate = ClipGet() ;Get the text from the clipboard to a variable called calldate

MsgBox(0, "Clipboard contains:", $calldate) ; Just for testing purpose

;$calldate2 = StringMid($calldate,8,10) ; Trim the text and create a new string with the trimmed string in, i call it calldate2
; If the date is always second, we can just stringsplit on space to get it. 
$calldate2 = StringSplit($calldate, " ")

MsgBox(0, "String middad characters is:", $calldate2[2]) ; Just for testing purpose

$calldate = $calldate2[2]
$CurrentDate = _NowCalcDate()
$sDate = StringReplace($calldate,"-","/")

$TotalDaysDifference = _DateDiff("d",$CurrentDate,$sDate)

$iTemp = Abs($TotalDaysDifference)

_FileReadToArray("Holidays.log",$aHolidays)

For $x = 1 to $iTemp
    $aTemp = StringSplit($sDate,"/")

    If _DateToDayOfWeekISO($aTemp[1],$aTemp[2],$aTemp[3]) > 4 Then 
        $iTemp -= 1
    Else
        ; Check Monday - Friday for a holiday.  If a match is found, reduce $iTemp by one since we won't be working that day.
        For $x = 1 to $aHolidays[0]
            If StringInStr($aHolidays[$x],$sDate) Then 
                msgbox(0,"","Holiday Found on: " & $sDate) ; Just for show, don't forget to remove this line from final version.
                $iTemp -= 1
            EndIf
        Next
    EndIf
    
    If $TotalDaysDifference < 0 Then
        $sDate = _DateAdd("d",1,$sDate)
    Else
        $sDate = _DateAdd("d",-1,$sDate)
    EndIf

Next

msgbox(1,"","Total Working Days difference: " & $iTemp)

And the holidays file, I called "Holidays.log", in the same folder, looks like this:

2008/05/26; Memorial Day
2008/07/04; Independance Day
2008/09/01; Labor Day
2008/10/13; Columbus Day
2008/11/11; Veterans Day
2008/11/27; Thanksgiving Day
2008/12/25; Christmas
2009/01/01; New Years
2009/01/19; Martin Luther King Day
2009/01/20; Inauguration Day
2009/02/16; Presidents Day
2009/05/25; Memorial Day
2009/07/04; Independance Day
2009/09/07; Labor Day
2009/10/12; COlumbus Day
2009/11/11; Veterans Day
2009/11/26; Thanksgiving
2009/12/25; Christmas
2010/01/01; New Years

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