Jump to content
Sign in to follow this  
Skeletor

Set Date From Variable (Solved)

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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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.

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
Share on other sites

@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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

@Skeletor
If you read your date from the Date picker control using GUICtrlRead(), what is the result? :)

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

Share this post


Link to post
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

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
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

"I need coffee to turn me back to a human"

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

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Jahar
      Hi All,
      Please help me on how to fetch date/time of last windows 10 system restore point using autoit? Basically its about querying the last restore point.
    • By TheDcoder
      Hi everyone,
      I am bit stumped as to why I am not able to set the time in the Date control
      #include <DateTimeConstants.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> Example() Func Example() GUICreate("My GUI get date", 200, 200, 800, 200) Local $idDate = GUICtrlCreateDate("1953/04/25", 10, 10, 185, 20, $DTS_TIMEFORMAT) ; to select a specific default format Local $sStyle = "HH:mm:ss" GUICtrlSendMsg($idDate, $DTM_SETFORMATW, 0, $sStyle) ; Set time Local $iRet = GUICtrlSetData($idDate, '13:33:37') ConsoleWrite('GUICtrlSetData returned ' & ($iRet = 1 ? 'success' : 'failure') & @CRLF) GUISetState(@SW_SHOW) ; Loop until the user exits. While GUIGetMsg() <> $GUI_EVENT_CLOSE WEnd MsgBox($MB_SYSTEMMODAL, "Time", GUICtrlRead($idDate)) EndFunc ;==>Example The documentation for GUICtrlSetData clearly mentions that it uses the same format as GUICtrlRead:
      But I get failure
      What gives?
      Thanks for the help in advance!
    • By Colduction
      Hi guys, i want to convert WebKit/Chrome timestamps that i've found in chrome cookie database to Human-Readable time (if possible, with UTC or GMT offset).
      Example of WebKit/Chrome timestamp: 13228999534132232 Output that i want: 2020-03-18 20:16:56 This site has a tool to do this work, but i wonder how to convert this time (this timestamp's time bases are 1600/01/01) to human-readable time:
      https://www.epochconverter.com/webkit
×
×
  • Create New...