Jump to content
Sign in to follow this  
Chimp

Date chooser

Recommended Posts

Even if I know about the existence of the _GUICtrlMonthCal* functions, here is a simple home made "Visual date chooser" function. It is in a raw draft format and can likely be emprooved, but I think that, all in all, this "skeleton" is not so bad. Maybe it can be of use...

; Calendar mini
#include <date.au3>
#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIEdit.au3>
#include <WinAPI.au3>
#include <StaticConstants.au3>

Local $aResult = _Peek_A_Date()
MsgBox(0, 'Result', (@error) ? 'No date selected' : _DateTimeFormat($aResult[0] & "/" & $aResult[1] & "/" & $aResult[2], 1), 3)

; ----------------------------------------------------------------------------------
; a simple 'visual' date peeker function
; parameters are optional
; YYYY and MM for initial desired calendar
; last parameter: if true (default) first weekday is moonday
;                 if false first weekday is sunday
; returns: on success a 3 elements array as [0]Year yyyy; [1]Monthh mm; [2]Day dd
;          on error set @error and returns 0
; -----------------------------------------------------------------------------------
Func _Peek_A_Date($iYear = @YEAR, $iMonth = @MON, $bISO = True)
    Local $hCalendar = GUICreate('', 210, 160, -1, -1, $WS_POPUPWINDOW, $WS_EX_TOOLWINDOW), $iDay = @MDAY
    Local $IdDate = GUICtrlCreateLabel('', 25, 4, 160, 20, $SS_CENTER, $GUI_WS_EX_PARENTDRAG), $idWeekDays = GUICtrlCreateLabel('', 2, 28, 206, 20)
    Local $idDays = GUICtrlCreateEdit('', 2, 50, 206, 108, BitOR($ES_READONLY, $ES_MULTILINE), 0), $hDays = GUICtrlGetHandle(-1)
    Local $iColor = 0x00ffd700, $Dummy = GUICtrlSetBkColor(-1, $iColor) + GUICtrlSetCursor(-1, 0)
    Local $Dummy = GUICtrlSetFont($idWeekDays, 12, -1, -1, 'courier new') + GUICtrlSetFont($idDays, 12, -1, -1, 'courier new') + GUICtrlSetFont($IdDate, 10, 700) ; 'courier new')
    Local $idPrevMonth = GUICtrlCreateButton('<', 2, 2, 18, 18), $idNextMonth = GUICtrlCreateButton('>', 190, 2, 18, 18)
    Local $sDaysOfWeek = '', $aDate[3] = [$iYear, $iMonth, $iDay], $aBounds, $aCharPos, $sLine, $s
    $iYear = 0
    For $i = 1 To 7
        $sDaysOfWeek &= StringLeft(_DateDayOfWeek($i, $DMW_LOCALE_SHORTNAME), 2) & " "
    Next
    $sDaysOfWeek = ($bISO) ? StringMid($sDaysOfWeek, 4) & StringLeft($sDaysOfWeek, 2) : StringLeft($sDaysOfWeek, 20)
    GUICtrlSetData($idWeekDays, $sDaysOfWeek)
    Local $sDays = ' 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31'
    GUISetBkColor($iColor)
    GUISetState(@SW_SHOW)
    Local $aBounds[2] = [($bISO) ?(_DateToDayOfWeekISO($aDate[0], $aDate[1], 1)) :(_DateToDayOfWeek($aDate[0], $aDate[1], 1)), _DateDaysInMonth($aDate[0], $aDate[1])]
    While WinActive($hCalendar)
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $GUI_EVENT_PRIMARYDOWN
                $aDate[2] = $s
                If _DateIsValid($aDate[0] & "/" & $aDate[1] & '/' & $aDate[2]) Then
                    ToolTip('')
                    GUIDelete($hCalendar)
                    Return SetError(0, 0, $aDate)
                EndIf
            Case $GUI_EVENT_MOUSEMOVE
                ; https://www.autoitscript.com/forum/topic/157899-get-text-word-under-the-mouse-pointer/?do=findComment&comment=1145318
                ; (portions of code by Malkey)
                $aCharPos = _GUICtrlEdit_CharFromPos($hDays, _WinAPI_GetMousePosX(True, $hDays), _WinAPI_GetMousePosY(True, $hDays))
                $sLine = _GUICtrlEdit_GetLine($hDays, $aCharPos[1])
                $aCharPos[0] -= _GUICtrlEdit_LineIndex($hDays, $aCharPos[1])
                $s = (StringMid($sLine, $aCharPos[0] + 1, 1) == " ") ? "" : StringRegExpReplace($sLine, "(?s)^.{0," & $aCharPos[0] & "}(?: |^)([^ ]*).*$", "\1")
                ToolTip(($s = "") ? "" : _DateTimeFormat($aDate[0] & "/" & $aDate[1] & "/" & $s, 2))
            Case $idPrevMonth
                $aDate = StringSplit(_DateAdd('M', -1, $aDate[0] & "/" & $aDate[1] & '/1'), '/', 2)
            Case $idNextMonth
                $aDate = StringSplit(_DateAdd('M', +1, $aDate[0] & "/" & $aDate[1] & '/1'), '/', 2)
        EndSwitch
        If $iYear <> $aDate[0] Or $iMonth <> $aDate[1] Then
            $iYear = $aDate[0]
            $iMonth = $aDate[1]
            $aBounds[0] = ($bISO) ?(_DateToDayOfWeekISO($aDate[0], $aDate[1], 1)) :(_DateToDayOfWeek($aDate[0], $aDate[1], 1))
            $aBounds[1] = _DateDaysInMonth($aDate[0], $aDate[1])
            GUICtrlSetData($idDays, StringLeft(StringLeft(StringFormat('%' & 18 & 's', ""), 3 * ($aBounds[0] - 1)) & _
                    StringTrimRight($sDays, (31 - $aBounds[1]) * 3) & StringFormat('%' & 40 & 's', ""), 126))
            GUICtrlSetData($IdDate, _DateToMonth($aDate[1], $DMW_LOCALE_LONGNAME) & " " & $aDate[0])
        EndIf ;
    WEnd
    GUIDelete($hCalendar)
    Return SetError(1, 0, 0)
EndFunc   ;==>_Peek_A_Date

 


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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  

  • Similar Content

    • By Jeep
      Here is an other UDF for string handling :
      Date handling
      _StringDateConvert:  convert a date from one format ("YMD", "MDY" or "DMY") to another.
      _StringIsDate:  checks if a date with a given format is valid
      String management
      _StringCount: count of occurrences that appear in a string
      _StringFormatBytesSize: formatting a dimension expressed in bytes (bytes) in MB, TB, ...)
      _StringIsEndingWith: check if a string end with some characters
      _StringIsStartingWith: check if a string start with some characters
      _StringJoinArray:  concatenate elements of an array to rebuild a string
      _StringPadLeft: filling a string with characters on the left
      _StringPadRight: fill a string with characters on the right
      _StringRemoveFrenchAccent: remove french accent
      _StringRemoveChars: deleting characters from a string
      _StringStrip: eliminate characters at the begin and/or at the  end of a string
      _StringTitleCaseFrench: capitalize the first letter of each word with elimination of french accents
      _StringWSClean: simple replacement of "White Spaces",  remove beginning and trailing spaces and multiple spaces removal
      Any comments, suggestions for improvement or constructive criticism are welcome.
      Below you will find the UDF and a demo program. 
       
       
      JPD_String.zip
    • By Skeletor
      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  
    • By VADemon
      I've encountered a problem with a single file where I cannot retrieve it's Date-time. So far my code has worked well for over 30 files, but this one is a mystery I cannot debug myself due to insufficient Au3 knowledge.
      In line 11 "_Date_Time_FileTimeToArray" is called and for this particular file it sets the @error to 10. I don't know what that error code means, but it's not set by the _Date functions themselves I think.
      Overall, it could be a problem caused by any of the functions below, how can I properly debug this? / Does anybody know a what's causing this?
      _WinAPI_CreateFile() / _Date_Time_GetFileTime() / _Date_Time_FileTimeToArray()
      Func _SetFileTimes($sFilePath) Local $monthNumber[13] = ["", "January", "February", "March", "April", "May", "Juny", "July", "August", "September", "October", "November", "December"] Local $dayNumber[7] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] Local $fHandle = _WinAPI_CreateFile($sFilePath, 2, 2) ; read-only ; may NOT return a valid date for some reason! TODO Local $fTagFILETIME = _Date_Time_GetFileTime($fHandle) _WinAPI_CloseHandle($fHandle) ; This will return an empty array if theres no valid date $fModTime = _Date_Time_FileTimeToArray($fTagFILETIME[2]) ; last Modified if @error <> 10 then Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] Global $prettyTimestamp = StringFormat("%s, %s %d, %04d %02d:%02d:%02d", $dayNumber[$weekday], $monthNumber[$month], $day, $year, $hour, $min, $sec) Global $uploadDate = StringFormat("%04d-%02d-%02d", $year, $month, $day) $fModTime = _Date_Time_FileTimeToArray(_Date_Time_FileTimeToLocalFileTime($fTagFILETIME[2])) ; last Modified Local $year = $fModTime[2] Local $month = $fModTime[0] Local $day = $fModTime[1] Local $hour = $fModTime[3] Local $min = $fModTime[4] Local $sec = $fModTime[5] Local $ms = $fModTime[6] Local $weekday = $fModTime[7] ; GetUnixTime accounts for Local time, hence feed it local time Global $unixTimestamp = _GetUnixTime($year &"/"& $month &"/"& $day &" "& $hour&":"& $min &":"& $sec) else Global $prettyTimestamp = "N/A" Global $uploadDate = "" Global $unixTimestamp = "N/A" endif endfunc  
      _GetUnixTime returned the year 1601 start date, showing that $fModTime is probably equal 0. (But Why?)
      The file reports these dates in Explorer, it's on local NTFS drive:
      Created: ‎‎Wednesday, ‎31. ‎Januar ‎2018, ‏‎18:55:02
      Modified: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
      Accessed: ‎Wednesday, ‎10. ‎Januar ‎2018, ‏‎12:39:23
    • By Jemboy
      Hi,
      Ones(some times twice) a month I get an e-mail with zip file, which has price updates from a supplier.
      I have to upload the file to an FTP to get it processed.
      When uploading the file, it will get "timestamped" with the time and date at which time the file was uploaded.
      Normally this is fine, because I mostly upload the file the same day.
      Sometimes it may take 1 or 2 days before I can upload the file.
      For historical purpose, I would like to have the file timestamped with the original date.
      I have tried using:  _FTP_Command ( $hFTPSession, "MFCT YYYYMMDDHHMMSS path") 
      however this command does not work or change the timestamp as I expected.
      Does anyone now a way how I can change the timestamp of a FTP-file?
    • By Siryx
      So what i need is a function that I pass a starting date into and it counts up to the current date. Example:
      2009-05-30
      2009-05-31
      2009-06-01
      ...
      2018-06-18
      How would I do that? I found the Date.au3 in the includes but I can't find anything close to a time object like I'm used to working with in Java. I just need some $date = setDate(2009-05-30) and from there I could just add a day every time. I need this to be in the very format I stated earlier and from what I can see everytime related to date and time is automatically changed to my german locale.
      €: I already tried setDate but instead it changed my PCs clock. Not quite what i was looking for  
×
×
  • Create New...