Skeletor Posted February 4, 2019 Posted February 4, 2019 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. expandcollapse popup#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 RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Subz Posted February 4, 2019 Posted February 4, 2019 Unsure if this is what you were after: Local $aDate = StringSplit(GUICtrlRead($Input1), "/") GUICtrlSetData($Date1, StringFormat("%04i/%02i/%02i", $aDate[3], $aDate[2], $aDate[1]))
Skeletor Posted February 4, 2019 Author Posted February 4, 2019 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 RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Subz Posted February 4, 2019 Posted February 4, 2019 (edited) 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 February 4, 2019 by Subz
Skeletor Posted February 4, 2019 Author Posted February 4, 2019 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 RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Subz Posted February 4, 2019 Posted February 4, 2019 Can you tell me what the output of _ArrayDisplay is, you will require #include <Array.au3> for this? Case $Button2 Local $aDate = StringSplit(GUICtrlRead($Input1), "/") _ArrayDisplay($aDate) GUICtrlSetData($Date1, StringFormat("%04i/%02i/%02i", $aDate[3], $aDate[2], $aDate[1]))
Skeletor Posted February 4, 2019 Author Posted February 4, 2019 Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Skeletor Posted February 4, 2019 Author Posted February 4, 2019 So looks like Ubound is required here,,. Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Subz Posted February 4, 2019 Posted February 4, 2019 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.
Skeletor Posted February 4, 2019 Author Posted February 4, 2019 Arrrgghhh, It got me there... Yes, you correct. Thanks for picking that up: Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Skeletor Posted February 5, 2019 Author Posted February 5, 2019 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 RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
FrancescoDiMuro Posted February 5, 2019 Posted February 5, 2019 @Skeletor If you set the date from the input box, how the date format in the input box looks like? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Skeletor Posted February 5, 2019 Author Posted February 5, 2019 Like this: Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Skeletor Posted February 5, 2019 Author Posted February 5, 2019 I pasted code in the OP. Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
FrancescoDiMuro Posted February 5, 2019 Posted February 5, 2019 (edited) @Skeletor If you read your date from the Date picker control using GUICtrlRead(), what is the result? Edited February 5, 2019 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Skeletor Posted February 5, 2019 Author Posted February 5, 2019 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) Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Skeletor Posted February 5, 2019 Author Posted February 5, 2019 Now @FrancescoDiMuro, How would I read it back to set the Date Picker? But, without changing the Region.. Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
FrancescoDiMuro Posted February 5, 2019 Posted February 5, 2019 (edited) @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 February 5, 2019 by FrancescoDiMuro Skeletor 1 Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette
Skeletor Posted February 5, 2019 Author Posted February 5, 2019 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: expandcollapse popup#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 RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Skeletor Posted February 5, 2019 Author Posted February 5, 2019 @FrancescoDiMuro - Thank you for helping with this. Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now