Sign in to follow this  
Followers 0
Iceman1988

Compare Date Pickers Values

8 posts in this topic

Hi :)

i'm experimenting with GUI's to try and learn more about it

So i've created a simple GUI with two date pickers in it and a Button, and what that button do when you press it is to check if the 1st date is superior or inferior than the 2nd date.

The problem is that "GuictrlRead" only considers the day, so if, for example, the 1st date is 09-10-2014 and the 2nd is 08-11-2014 the return is that the 1st date is superior.

The strangest thing is that if i put the "GuictrlRead" in a msgbox, it shows me the whole date :(

Did anybody had this problem before?

Here's the code:

#include <ButtonConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <MsgBoxConstants.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 633, 447, 192, 124)
$Date1 = GUICtrlCreateDate("", 64, 56, 105, 49,$DTS_SHORTDATEFORMAT)
$Date2 = GUICtrlCreateDate("", 64, 128, 105, 41,$DTS_SHORTDATEFORMAT)
$Button1 = GUICtrlCreateButton("Button1", 64, 216, 113, 41, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $Button1

         If GUICtrlRead($Date1) < GUICtrlRead($Date2) Then
            MsgBox($MB_SYSTEMMODAL, "Data", "Data escolhida Inferior a 1.")
         Elseif GUICtrlRead($Date1) > GUICtrlRead($Date2) Then
            MsgBox($MB_SYSTEMMODAL, "Data", "Data escolhida superior a 1.")
         Else
            MsgBox($MB_SYSTEMMODAL, "Data", "Datas iguais.")
        EndIf

    EndSwitch
WEnd

Thanks a lot in advance and sorry fo the bad english

 

Share this post


Link to post
Share on other sites



Iceman1988,

 

Did anybody had this problem before?

It pops up about once a week! :D

You are comparing 2 strings which AutoIt automatically converts to numbers for the comparison - and all that is left when this conversion occurs are the digits up to the first non-digit, i.e. the day number. You will either have to convert the returned string into a format which will read as a number or into the format used by _DateDiff. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23

Thanks for your reply

 

It pops up about once a week! :D

 

Really?? damn i suck at searching stuff in the forum :P

I'm a newbie still learning :ermm: can you post some examples on how to do the convertion or the usage of _datediff

Thanks in advance

Share this post


Link to post
Share on other sites

Iceman1988,

It is the fact that AutoIt converts comparisons to numbers automatically that keeps being raised, not anything specific to date pickers - so yoru searching might not be that bad after all. ;)

Here is an example script showing the 2 ways you might compare the selected dates. Method 1 creates a valid number which can be compared directly, while Method 2 uses _DateDiff. I have also demonstrated 2 ways to get the date reformatted: first a split and then a RegEx:

#include <GUIConstantsEx.au3>
#include <Date.au3>

$hGUI = GUICreate("Test", 500, 500)

$cDate1 = GUICtrlCreateDate("", 10, 20, 100, 20, $DTS_SHORTDATEFORMAT)
$cDate2 = GUICtrlCreateDate("", 10, 50, 100, 20, $DTS_SHORTDATEFORMAT)
$cButton1 = GUICtrlCreateButton("Method 1", 10, 100, 80, 30)
$cButton2 = GUICtrlCreateButton("Method 2", 10, 150, 80, 30)

GUISetState(@SW_SHOW)

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cButton1

            $sDate_1 = GUICtrlRead($cDate1)
            $aSplit = StringSplit($sDate_1, "/")
            $sDate_1 = $aSplit[3] & $aSplit[2] & $aSplit[1]

            $sDate_2 = GUICtrlRead($cDate2)
            $aSplit = StringSplit($sDate_2, "/")
            $sDate_2 = $aSplit[3] & $aSplit[2] & $aSplit[1]

            MsgBox($MB_SYSTEMMODAL, "Converted Dates", $sDate_1 & @CRLF & $sDate_2)

            If $sDate_1 < $sDate_2 Then
                MsgBox($MB_SYSTEMMODAL, "Data", "Data escolhida Inferior a 1.")
            ElseIf $sDate_1 > $sDate_2 Then
                MsgBox($MB_SYSTEMMODAL, "Data", "Data escolhida superior a 1.")
            Else
                MsgBox($MB_SYSTEMMODAL, "Data", "Datas iguais.")
            EndIf

        Case $cButton2
            $sDate_1 = StringRegExpReplace(GUICtrlRead($cDate1), "^(\d\d)\/(\d\d)\/(\d\d)$", "20$3$2$1")
            $sDate_2 = StringRegExpReplace(GUICtrlRead($cDate2), "^(\d\d)\/(\d\d)\/(\d\d)$", "20$3$2$1")

            MsgBox($MB_SYSTEMMODAL, "Converted Dates", $sDate_1 & @CRLF & $sDate_2)

            $iDateDiff = _DateDiff("D", $sDate_1, $sDate_2)
            If $iDateDiff > 0 Then
                MsgBox($MB_SYSTEMMODAL, "Data", "Data escolhida Inferior a 1.")
            ElseIf $iDateDiff < 0 Then
                MsgBox($MB_SYSTEMMODAL, "Data", "Data escolhida superior a 1.")
            Else
                MsgBox($MB_SYSTEMMODAL, "Data", "Datas iguais.")
            EndIf

    EndSwitch
WEnd
Be aware that the above is only valid for dates from 2000 onwards - if you want to compare dates from before then we need to add another step to get the correct 19/20 prefix. ;)

Please ask if you have any questions. :)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Hi melba23

Thanks for both your time and replies :)

I'm getting an array error on the 1st method

"D:Documents and Settingsxctci02DesktopRTEafdasdf.au3" (21) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$sDate_1 = $aSplit[3] & $aSplit[2] & $aSplit[1]
$sDate_1 = ^ ERROR

the 2nd method you showed me i had already tried using datediff, without the convertion of the dates you did, but the result was the same

it returns only the msgbox saying the dates are equal no matter what date is in each picker :(

this seams a lot more complex than i though :(

Share this post


Link to post
Share on other sites

Iceman1988,

As I see you use a non-English OS you quite possibly have a different date delimiter. I see "09 / 10 / 14" in the picker - what do you see? If it is different then just replace the "/" in lines 20, 24, 38 and 39 with the delimiter that you see. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23

Thanks for both your time and replies :)

The 1st method worked perfectly just had to invert the "<" & ">" in the if's for the check to in the correct order :)

the 2nd method although continues to return all dates as equal even after changing the delimiter :ermm:

I think the problem is in the dates because i used _dateisvalid and the return was not valid

Share this post


Link to post
Share on other sites

Iceman1988,

Sorry, I missed out a couple of delimiters. :blush:

Change the RegEx lines to read:

$sDate_1 = StringRegExpReplace(GUICtrlRead($cDate1), "^(\d\d)\/(\d\d)\/(\d\d)$", "20$3/$2/$1") ; Note the addition 0f a couple of /
$sDate_2 = StringRegExpReplace(GUICtrlRead($cDate2), "^(\d\d)\/(\d\d)\/(\d\d)$", "20$3/$2/$1")
Now the dates should be properly formatted for _DateDiff. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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