Jump to content

Set Date From Variable (Solved)


 Share

Recommended Posts

Hey All,

I'm trying to set the date using a variable.
Basically, I set the date into the input box, then I change the input box, say, I change the year.
Then I set what I typed into the input box into the Date Picker.

This is a demo code.

#include <ButtonConstants.au3> ;Start GUI includes
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiDateTimePicker.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$Date1 = GUICtrlCreateDate("2019/02/02 23:16:26", 80, 64, 186, 21, $DTS_SHORTDATEFORMAT)

$Input1 = GUICtrlCreateInput("Input1", 80, 152, 185, 21)
$Button1 = GUICtrlCreateButton("Set data", 176, 96, 75, 25)
$Button2 = GUICtrlCreateButton("Read from input", 176, 200, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $Read = GUICtrlRead($Date1)
            GUICtrlSetData($Input1, $Read)
            MsgBox(-1, "", $Read)
        Case $Button2
            $Read = GUICtrlRead($Input1)

            $New_date = StringReplace($Read, "/", "")
            $DAY = StringLeft($New_date, 2)
            $MON = StringMid($New_date, 4, 3)
            $YEAR = StringRight($New_date, 4)

            MsgBox(-1, "", $DAY & $MON & $YEAR)

            ;_GUICtrlDTP_SetFormat($hWndDate, "yyyy/MM/dd")
            $DateFormate = ($DAY & " " & $MON & " " & $YEAR)
            $DTM_SETFORMAT_ = 0x1032 ; $DTM_SETFORMATW
            GUICtrlSendMsg($Date1, $DateFormate, 0, "MM/dd/yyyy")

    EndSwitch
WEnd

 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

Tried it, didnt work, also had to change the forward slashes to backward slashes. However I must say I didnt think about the StringFormat. 

I know GuiCtrlSetData is suppose to change the Date Picker, but its not. 
Also tried, _GUICtrlDTP_SetFormat and I got no Date been changed.
Tried to change to the $DTS_SHORTDATEFORMAT stlye thinking it could have been this that caused it, but to no avail. 

There is a combination I'm not doing... I know this can be done. 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

Works fine for me, see code changes below:

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $Read = GUICtrlRead($Date1)
            GUICtrlSetData($Input1, $Read)
        Case $Button2
            Local $aDate = StringSplit(GUICtrlRead($Input1), "/")
            GUICtrlSetData($Date1, StringFormat("%04i/%02i/%02i", $aDate[3], $aDate[2], $aDate[1]))
    EndSwitch
WEnd

 

Edited by Subz
Link to comment
Share on other sites

Did you include the #include <Array.au3>??
I did, but still getting this error:

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
GUICtrlSetData($Date1, StringFormat("%04i/%02i/%02i", $aDate[3], $aDate[2], $aDate[1]))
GUICtrlSetData($Date1, StringFormat("%04i/%02i/%02i", ^ ERROR

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

I think its because your regional settings are different to mine, when I use the code above, $Date1 = 2/02/2019, when I click Set data button $Input1 = 2/02/2019, I then modify this to 2/02/2024 and click Read from Input, which then sets the $Date1 = 2/02/2024.

Link to comment
Share on other sites

Thanks @Subz for helping. Is there a way to retrieve this information without having to change the Region Settings. 
Because this messes with Excel as well.. 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

That's what the code does... the first button reads the date from the Date Picker.

Case $Button1
            $Read = GUICtrlRead($Date1)
            GUICtrlSetData($Input1, $Read)
            MsgBox(-1, "", $Read)

image.png.2905cd82b6cb31ca6fb6d39b28a7cc8f.png

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

@Skeletor
If your input date will always be in the format DD MMM YYYY, then you can use something like this:

#include <Array.au3>
#include <Date.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>

Global $strInputDate = "05 Feb 2019", _
       $arrResult, _
       $strDateTime, _
       $strOutputDate


$arrResult = StringRegExp($strInputDate, "^(\d{2})\s{1}([A-Z][a-z]{2})\s{1}(\d{4})$", $STR_REGEXPARRAYMATCH)
$strDateTime = StringFormat("%04i/%02i/%02i", $arrResult[2], _GetMonthInDigits($arrResult[1]), $arrResult[0])

$strOutputDate = _DateTimeFormat($strDateTime, 2)
If @error Then
    ConsoleWrite("Error while the date conversion! Error: " & @error & @CRLF)
    Exit
Else
    MsgBox($MB_ICONINFORMATION, "", $strOutputDate)
EndIf


Func _GetMonthInDigits($strMonth)

    Local $arrMonths[12] = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]

    For $i = 0 To UBound($arrMonths) - 1 Step 1
        If $arrMonths[$i] = $arrResult[1] Then Return ($i + 1)
    Next

EndFunc

Using _DateTimeFormat, and passing the date "splitted" in the format YYYY/MM/DD, you'll always have your output date in the format of the local regional settings of the PC :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

I placed your code into my demo code. You mentioned something that triggered my thinking. 

1 hour ago, FrancescoDiMuro said:

Using _DateTimeFormat, and passing the date "splitted" in the format YYYY/MM/DD, you'll always have your output date in the format of the local regional settings of the PC

With that I used the "raw" format to give you this:

image.png.ef49708bcc4fe64c72076bffbe4d257a.png

#include <ButtonConstants.au3> ;Start GUI includes
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>
#include <GuiDateTimePicker.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$Date1 = GUICtrlCreateDate("2019/02/02 23:16:26", 80, 64, 186, 21, $DTS_SHORTDATEFORMAT)

$Input1 = GUICtrlCreateInput("Input1", 80, 152, 185, 21)
$Button1 = GUICtrlCreateButton("Set data", 176, 96, 75, 25)
$Button2 = GUICtrlCreateButton("Read from input", 176, 200, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $arrResult, _
        $strDateTime, _
        $strOutputDate

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $Read = GUICtrlRead($Date1)
            GUICtrlSetData($Input1, $Read)
            MsgBox(-1, "", $Read)
        Case $Button2
            $strInputDate = GUICtrlRead($Input1)


            $arrResult = StringRegExp($strInputDate, "^(\d{2})\s{1}([A-Z][a-z]{2})\s{1}(\d{4})$", $STR_REGEXPARRAYMATCH)
            $strDateTime = StringFormat("%04i/%02i/%02i", $arrResult[2], _GetMonthInDigits($arrResult[1]), $arrResult[0])

            $strOutputDate = _DateTimeFormat($strDateTime, 2)
            If @error Then
                ConsoleWrite("Error while the date conversion! Error: " & @error & @CRLF)
                Exit
            Else
                GuiCtrlSetData($Date1, $strDateTime)
            EndIf

    EndSwitch
WEnd

 

 

Kind Regards
Skeletor

"Coffee: my defense against going postal."

Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI

Link to comment
Share on other sites

  • Skeletor changed the title to Set Date From Variable (Solved)

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
 Share

×
×
  • Create New...