Jump to content
Sign in to follow this  
Freedom1

How to format Date-Time-Picker Selected Date?

Recommended Posts

Freedom1

Hello All,

I'm confused in how to use the DTP selected date. Here's my situation.

I have a DTP control on my form. The date in the DTP is displayed as "Wed, April 22, 2009". The user then presses a button and now I want to format that selected date into another text input control and several $variables. I can convert to a text input control using the following code although it looks like a hack ($Date1F is the DTP control and $Date1Name is the text input control).

$style = "MMddyy"

GUICtrlSendMsg($Date1F, $DTM_SETFORMAT_, 0, $style)

GUICtrlSetData($Date1Name, GUICtrlRead($Date1F))

Now I want to change the format of the DTP selected date to various $variables in the following formats.

$var1 - mmddyy 042209

$var2 - yyyymmdd 20090422

$var3 - mmm dd April 22

$var4 - yymmdd-constant 090422-SummaryReport

The GUICtrlRead returns the selected date in the exact same way as it is displayed in the DTP.

There has to be a cleaner and more efficient way to convert the formats of the dates. I've checked the forums and tried _DateTimeFormat but couldn't get it to work properly.

Please assist and point me in the right direction.

Freedom1

Share this post


Link to post
Share on other sites
Melba23

Freedom1,

I think this does what you want:

#include <GUIConstantsEx.au3>
#Include <GuiDateTimePicker.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

Global $fFlag = False, $aTime[8]

$hGUI = GUICreate("Test", 500, 500)

$hDTP = _GUICtrlDTP_Create($hGUI, 10, 10)

$hLabel1 = GUICtrlCreateLabel("", 10,  50, 300, 20)
$hLabel2 = GUICtrlCreateLabel("", 10,  80, 300, 20)
$hLabel3 = GUICtrlCreateLabel("", 10, 110, 300, 20)
$hLabel4 = GUICtrlCreateLabel("", 10, 140, 300, 20)

GUIRegisterMsg($WM_NOTIFY, "MY_WM_NOTIFY")

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    
    If $fFlag Then
        GUICtrlSetData($hlabel1, StringFormat("%02s", $aTime[0]) & StringFormat("%02s", $aTime[1]) & StringRight($aTime[2], 2))
        GUICtrlSetData($hlabel2, $aTime[2] & StringFormat( "%02s", $aTime[0]) & StringFormat("%02s", $aTime[1]))
        GUICtrlSetData($hlabel3, _DateToMonth($aTime[0]) & " " & StringFormat("%02s", $aTime[1]))
        GUICtrlSetData($hlabel4, StringRight($aTime[2], 2) & StringFormat("%02s", $aTime[1]) & StringFormat("%02s", $aTime[0]) & "-SummaryReport")
        $fFlag = False
    Endif
    
WEnd

Func MY_WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $tBuffer, $tBuffer2

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hDTP
            Switch $iCode
                Case $DTN_DATETIMECHANGE; Sent by a date and time picker (DTP) control whenever a change occurs
                    $tInfo = DllStructCreate($tagNMDATETIMECHANGE, $ilParam)
                    $aTime = _Date_Time_SystemTimeToArray($tInfo)
                    $fFlag = True
                    Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc  ;==>MY_WM_NOTIFY

The labels wil change when the DTP changes. The formatting part is in the GUICtrlSetData statements.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Melba23

Freedom1,

A different way to do it, using the bult-in Date control. You have to press the button to get the labels to update here, but that might be seen as an feature rather than a bug: ;-)

#include <GUIConstantsEx.au3>
#include <DateTimeConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

$hGUI = GUICreate("Test", 500, 500)

$date = GUICtrlCreateDate("", 10, 10, 200, 20)

$hLabel1 = GUICtrlCreateLabel("", 10,  50, 300, 20)
$hLabel2 = GUICtrlCreateLabel("", 10,  80, 300, 20)
$hLabel3 = GUICtrlCreateLabel("", 10, 110, 300, 20)
$hLabel4 = GUICtrlCreateLabel("", 10, 140, 300, 20)

$hButton = GUICtrlCreateButton("Display", 10, 200, 80, 30)

GUISetState()

GUICtrlSendMsg($date, 0x1032, 0, "ddMMMMyy")

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hButton
            GUICtrlSendMsg($date, 0x1032, 0, "MMddyy")
            GUICtrlSetData($hlabel1, GUICtrlRead($date))
            GUICtrlSendMsg($date, 0x1032, 0, "yyyyMMdd")
            GUICtrlSetData($hlabel2, GUICtrlRead($date))
            GUICtrlSendMsg($date, 0x1032, 0, "dd MMMM")
            GUICtrlSetData($hlabel3, GUICtrlRead($date))
            GUICtrlSendMsg($date, 0x1032, 0, "yyMMdd")
            GUICtrlSetData($hlabel4, GUICtrlRead($date) & "-Summary Report")
            GUICtrlSendMsg($date, 0x1032, 0, "ddMMMMyy")
    EndSwitch
    
WEnd

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Freedom1

Melba23,

Thanks for your response. Just got back on it today. I prefer to use option 1. Using option 2 would add another button which may clutter the screen or cause confustion. Choices are good!

Its exactly what I'm looking for. Thank you. I've seen the "MY_WM_NOTIFY" function in other snippets of AutoIt code in the forums. Is there a particular thread or location that I can read about the uses of it?

Thanks again,

Freedom1

Share this post


Link to post
Share on other sites
Melba23

Freedom1,

Delighted to help.

Read about GUIRegisterMsg in the Help file to learn more about how to harness Windows messages. Other than that it is a question of learning from examples - just like I did!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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  

×