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

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

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 nacerbaaziz
      it recomended to use the builten function
      _DateDiff
       
       
       
      hello autoit team
      i made a simple function to help me get the difference between to date
      i liked to share it with you
      the paramatrs of this function
      is firstdate and lastedate
      the two params is as the following
      year/month/day hours:minuts:seconds
      here is the function
       
      #include <date.au3> func _dateBetween($s_firstDate, $s_lastDate = default) if not (StringRegExp($s_firstDate, "((\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2}))", 0) = 1) then Return SetError(1, 0, 0) if $s_lastDate = default then $s_lastDate = @year & "/" & @mon & "/" & @mday & " " & @hour & ":" & @min & ":" & @sec if not (StringRegExp($s_lastDate, "((\d{4})\/(\d{2})\/(\d{2})\s(\d{2})\:(\d{2})\:(\d{2}))", 0) = 1) then Return SetError(2, 0, 0) local $a_FirstSplitDate = StringSplit(StringRegExpReplace($s_firstDate, "((.*)\s(.*))", "$2"), "/", 2) local $a_FirstSplitTime = StringSplit(StringRegExpReplace($s_firstDate, "((.*)\s(.*))", "$3"), ":", 2) local $a_lastSplitDate = StringSplit(StringRegExpReplace($s_lastDate, "((.*)\s(.*))", "$2"), "/", 2) local $a_LastSplitTime = StringSplit(StringRegExpReplace($s_lastDate, "((.*)\s(.*))", "$3"), ":", 2) local $i_firstDateValue = _DateToDayValue($a_FirstSplitDate[0], $a_FirstSplitDate[1], $a_FirstSplitDate[2]) local $i_LastDateValue = _DateToDayValue($a_LastSplitDate[0], $a_LastSplitDate[1], $a_LastSplitDate[2]) if $i_firstDateValue > $i_LastDateValue then Return SetError(3, 0, 0) local $i_totalDays = $i_LastDateValue-$i_firstDateValue if not ($i_totalDays = 0) then if $a_FirstSplitTime[0] <= $a_lastSplitTime[0] then $i_totalDays += 1 $a_lastSplitTime[0] -= $a_FirstSplitTime[0] elseIf $a_FirstSplitTime[0] > $a_lastSplitTime[0] then if $i_totalDays > 0 then $i_totalDays -= 1 $a_FirstSplitTime[0] -= $a_lastSplitTime[0] endIf endIf endIf local $i_totalYears = 0 if $i_totalDays >= 365 then While $i_totalDays >= 365 $i_totalYears += 1 $i_totalDays -= 365 Wend endIf local $i_TotalMonths = 0 if $i_totalDays >= 30 then While $i_totalDays >= 30 $i_totalMonths += 1 $i_totalDays -= 30 Wend endIf local $i_totalWeeks = 0 if $i_totalDays >= 7 then While $i_totalDays >= 7 $i_totalWeeks += 1 $i_totalDays -= 7 Wend endIf $i_totalSeconds = ((($a_FirstSplitTime[0]*60)*60)+($a_FirstSplitTime[1]*60)+($a_FirstSplitTime[2]))-((($a_lastSplitTime[0]*60)*60)+($a_lastSplitTime[1]*60)+($a_LastSplitTime[2])) local $minus = "" if $i_totalSeconds < 0 then $minus = "-" $i_totalSeconds = StringReplace($i_totalSeconds, "-", "") endIf local $i_totalHours = 0 if $i_totalSeconds >= 3600 then While $i_totalSeconds >= 3600 $i_totalHours += 1 $i_totalSeconds -= 3600 Wend endIf local $i_totalminuts = 0 if $i_totalSeconds >= 60 then While $i_totalSeconds >= 60 $i_totalminuts += 1 $i_totalSeconds -= 60 Wend endIf local $a_arrayReturn[7] $a_arrayReturn[0] = $i_totalYears $a_arrayReturn[1] = $i_totalMonths $a_arrayReturn[2] = $i_totalWeeks $a_arrayReturn[3] = $i_totalDays $a_arrayReturn[4] = $minus & $i_totalHours $a_arrayReturn[5] = $minus & $i_totalminuts $a_arrayReturn[6] = $minus & $i_totalSeconds return $a_arrayReturn endFunc  
      here is an example
       
       
      $a_calc = _dateBetween("2015/02/12 23:00:05", "2030/02/12 23:25:50") msgBox(64, "result", StringFormat("the difference is : %i years and %i months and %i weeks and %i days and %i hours and %i minuts and %i seconds", $a_calc[0], $a_calc[1], $a_calc[2], $a_calc[3], $a_calc[4], $a_calc[5], $a_calc[6]))  
      am waiting for your comments
      with my greetings
    • 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!
×
×
  • Create New...