Sign in to follow this  
Followers 0
Chimp

Date chooser

1 post in this topic

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  
Followers 0

  • Similar Content

    • ur
      By ur
      I have date in the string format as "DD-MM-YYYY".
       
      I need to get yesterday's date from it.
      I tried converting this from _DateTimeFormat but not working.
      Is there any direct UDF available to get this.?
    • ur
      By ur
      In some systems the date is displayed in the Taskbar as
      10-01-2018
      And in some systems it is like, 1/10/2018
      And in some it is, 10-Jan-2018
      And in different formats.
       
      I wrote my code to convert 1/10/2018 to 10-01-2018
      Func TodaysDate() $NewDate = _DateTimeFormat(_NowCalcDate(),1) $Array = StringSplit( $NewDate , ',' ) _ArrayDelete($Array, 0) _ArrayDelete($Array, 0) $Array1 = StringSplit($Array[0],' ') RemoveEmptyArrayLines($Array1) ;Will return the present day's date with format dd-mmm-yyyy ;$Date = StringStripWS($Array1[2]&"-"&StringLeft($Array1[1], 3)&"-"&$Array[1], $STR_STRIPALL) $Date = StringStripWS($Array1[2]&"-"&changeDateformat(StringLeft($Array1[1], 3))&"-"&$Array[1], $STR_STRIPALL) return $Date EndFunc Func RemoveEmptyArrayLines(ByRef $arrLines) $intCount = 1 While $intCount < UBound($arrLines) $arrLines[$intCount] = StringStripWS($arrLines[$intCount],$STR_STRIPLEADING + $STR_STRIPTRAILING) If StringLen($arrLines[$intCount])=0 Then _ArrayDelete($arrLines, $intCount) $intCount = $intCount - 1 EndIf $intCount = $intCount + 1 WEnd EndFunc ;To convert mmm to mm format Func changeDateformat($sText) Local $sMsg = StringStripWS($sText, $STR_STRIPALL) Switch $sMsg Case "Jan" $sMsg = "01" Case "Feb" $sMsg = "02" Case "Mar" $sMsg = "03" Case "Apr" $sMsg = "04" Case "May" $sMsg = "05" Case "Jun" $sMsg = "06" Case "Jul" $sMsg = "07" Case "Aug" $sMsg = "08" Case "Sep" $sMsg = "09" Case "Oct" $sMsg = "10" Case "Nov" $sMsg = "11" Case "Dec" $sMsg = "12" EndSwitch return $sMsg EndFunc But again it will work on machines only with 1/10/2018.
      Is there any direct function which will give only in the format 10-01-2018, whatever the system settings is.?
      I tried all the arguments for _DateTimeFormat, but showing machine dependent settings only.
      Google given these two links in AutoIT but these are also for specific formats only,like mine.
       
      Please suggest.
    • kawliga751
      By kawliga751
      I have established how to get tomorrows date however I can't seem to figure out how to format the date into my required format of MM/DD/YYYY.  
      I have this:
      $today=_DateToDayValue(@YEAR,@MON,@MDAY) Dim $Y, $M, $D $tomorrow=_DayValueToDate($today+1, $Y, $M, $D) The _DateToDayValue seems to have a fixed format.  I've also tried a :
      $vardate = _DateAdd( 'd',1, _NowCalcDate()) with a "StringSplit" and/or StringFormat but the _NowCalcDate seems to also be fixed to a YYYY/MM/DD format.
      I'm sure I'm missing something simple but am at a loss.
      Any help would be MUCH appreciated
       
       
    • ur
      By ur
      To get the current time stamp, I got the below code.
      #include <Date.au3> #include <MsgBoxConstants.au3> #include <WindowsConstants.au3> #RequireAdmin ; Under Vista the Windows API "SetSystemTime" may be rejected due to system security $td = _Date_Time_GetSystemTime() $td = _Date_Time_SystemTimeToDateTimeStr($td) $td = StringReplace($td, " ", "_") $td = StringReplace($td, ":", "_") MsgBox(0,"",$td) But it is not giving the date or time of the timezone where the system is there.
      Please suggestt
    • ur
      By ur
      When I tried the below code, I am getting the date in mm-dd-yyyy format.
      But I require dd-mm-yyyy format.
      Can anyone suggest how to do that.
      Func TodaysDate() return StringReplace(_DateTimeFormat(_NowCalc(), 2), "/", "-") EndFunc Earlier I tried dd-mmm-yyyy format and it is working with below code.
      Func TodaysDate() $NewDate = _DateTimeFormat(_NowCalcDate(),1) $Array = StringSplit( $NewDate , ',' ) _ArrayDelete($Array, 0) _ArrayDelete($Array, 0) $Array1 = StringSplit($Array[0],' ') RemoveEmptyArrayLines($Array1) $Date = StringStripWS($Array1[2]&"-"&StringLeft($Array1[1], 3)&"-"&$Array[1], $STR_STRIPALL) return $Date EndFunc Which is unnecessarily complicated I feel., the approach
      Now I need format of dd-mm-yyyy.
      Can anyone suggest how to do this.