GettingStarted
GettingStarted

Reputation: 7605

How do I get the date of the first day of a week?

#include <GUIConstantsEx.au3>
#include <GuiMonthCal.au3>
#include <WindowsConstants.au3>

Global $g_idMemo

Example()

Func Example()
    Local $idMonthCal
    ; Create GUI
    GUICreate("Month Calendar Get First DOW String", 400, 300)
    $idMonthCal = GUICtrlCreateMonthCal("", 4, 4, -1, -1, $WS_BORDER, 0x00000000)
    ; Create memo control
    $g_idMemo = GUICtrlCreateEdit("", 4, 168, 392, 128, 0)
    GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
    GUISetState(@SW_SHOW)
    ; Get/Set first DOW
    _GUICtrlMonthCal_SetFirstDOW($idMonthCal, 0)
    MemoWrite("First DOW : " & _GUICtrlMonthCal_GetFirstDOWStr($idMonthCal))
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc

; Write message to memo
Func MemoWrite($sMessage)
    GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
EndFunc

This line just returns Monday. I want it to return Monday October 10, 2016. How can I make it do that?

MemoWrite("First DOW" & _GUICtrlMonthCal_GetFirstDOWStr($idMonthCal))

Upvotes: 2

Views: 169

Answers (2)

Timothy Bomer
Timothy Bomer

Reputation: 510

There are a few ways you could do this. I'll show you one way.

In an attempt to keep your code nice and neat, I would put everything into a separate function and call it when needed. Today, we're going to build one and call it:

analyzeDate()

What this function will do will is count how many days ago the last Monday was, then get the date. Using the two pre-defined functions would return the date in the format YYYY/MM/DD. In order to get the results you're looking for, we can split the date into the year, month, and day (Respectively), analyze the month, then set up the date in the format you'd like.

See the example function below.

Func analyzeDate()                                                                  
    $iLastMon = _DateToDayOfWeek(@YEAR, @MON, @MDAY) - 2                            
    ;MsgBox(0,"","Last Monday was " & $iLastMon & " days ago." & @LF)               
    $sLastMon = _DateAdd("D", ($iLastMon * -1), @YEAR & "/" & @MON & "/" & @MDAY)   
    ;MsgBox(0,"","Last Monday was " & $sLastMon & @LF)                              
    global $tDate = ""                                                              

    $newDate = StringSplit($sLastMon, "/")                                          
    If $newDate[2] = 1 Then                                                         
        $tDate = "January " & $newDate[3] & ", " & $newDate[1]                      
    ElseIf $newDate[2] = 2 Then                                                     
        $tDate = "Febuary " & $newDate[3] & ", " & $newDate[1]                      
    ElseIf $newDate[2] = 3 Then                                                     
        $tDate = "March " & $newDate[3] & ", " & $newDate[1]                        
    ElseIf $newDate[2] = 4 Then                                                     
        $tDate = "April " & $newDate[3] & ", " & $newDate[1]                        
    ElseIf $newDate[2] = 5 Then                                                     
        $tDate = "May " & $newDate[3] & ", " & $newDate[1]                          
    ElseIf $newDate[2] = 6 Then                                                     
        $tDate = "June " & $newDate[3] & ", " & $newDate[1]                         
    ElseIf $newDate[2] = 7 Then                                                     
        $tDate = "July " & $newDate[3] & ", " & $newDate[1]                         
    ElseIf $newDate[2] = 8 Then                                                     
        $tDate = "August " & $newDate[3] & ", " & $newDate[1]                       
    ElseIf $newDate[2] = 9 Then                                                     
        $tDate = "September " & $newDate[3] & ", " & $newDate[1]                    
    ElseIf $newDate[2] = 10 Then                                                    
        $tDate = "October " & $newDate[3] & ", " & $newDate[1]
    ElseIf $newDate[2] = 11 Then
        $tDate = "November " & $newDate[3] & ", " & $newDate[1]
    ElseIf $newDate[2] = 12 Then
        $tDate = "December " & $newDate[3] & ", " & $newDate[1]
    Else
        MsgBox(16,"ERROR", "There was an issue analyzing the date!")
        $tDate = "ERROR"
    EndIf
EndFunc

Now, you can call this function right before you call MemoWrite(), and add the $tDate variable at the end of the parameter of MemoWrite().

EXAMPLE:

analyzeDate()
MemoWrite("First DOW : " & _GUICtrlMonthCal_GetFirstDOWStr($idMonthCal) & " " & $tDate)

Now, your full code would look something similar to this:

Calendar.au3

#include <GUIConstantsEx.au3>
#include <GuiMonthCal.au3>
#include <WindowsConstants.au3>
; NEW =====================
#include <Date.au3>     ; =
; =========================

Global $g_idMemo

Example()

Func Example()
    Local $idMonthCal
    ; Create GUI
    GUICreate("Month Calendar Get First DOW String", 400, 300)
    $idMonthCal = GUICtrlCreateMonthCal("", 4, 4, -1, -1, $WS_BORDER, 0x00000000)
    ; Create memo control
    $g_idMemo = GUICtrlCreateEdit("", 4, 168, 392, 128, 0)
    GUICtrlSetFont($g_idMemo, 9, 400, 0, "Courier New")
    GUISetState(@SW_SHOW)
    ; Get/Set first DOW
    _GUICtrlMonthCal_SetFirstDOW($idMonthCal, 0)
    ; NEW =============
    analyzeDate()   ; =
    ; =================
    MemoWrite("First DOW : " & _GUICtrlMonthCal_GetFirstDOWStr($idMonthCal) & " " & $tDate) ; ADDED:  & " " & $tDate
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE
    GUIDelete()
EndFunc

; Write message to memo
Func MemoWrite($sMessage)
    GUICtrlSetData($g_idMemo, $sMessage & @CRLF, 1)
EndFunc

; NEW =================================================================================
Func analyzeDate()                                                                  ; =
    $iLastMon = _DateToDayOfWeek(@YEAR, @MON, @MDAY) - 2                            ; =
    ;MsgBox(0,"","Last Monday was " & $iLastMon & " days ago." & @LF)               ; =
    $sLastMon = _DateAdd("D", ($iLastMon * -1), @YEAR & "/" & @MON & "/" & @MDAY)   ; =
    ;MsgBox(0,"","Last Monday was " & $sLastMon & @LF)                              ; =
    global $tDate = ""                                                              ; =
                                                                                    ; =
    $newDate = StringSplit($sLastMon, "/")                                          ; =
    If $newDate[2] = 1 Then                                                         ; =
        $tDate = "January " & $newDate[3] & ", " & $newDate[1]                      ; =
    ElseIf $newDate[2] = 2 Then                                                     ; =
        $tDate = "Febuary " & $newDate[3] & ", " & $newDate[1]                      ; =
    ElseIf $newDate[2] = 3 Then                                                     ; =
        $tDate = "March " & $newDate[3] & ", " & $newDate[1]                        ; =
    ElseIf $newDate[2] = 4 Then                                                     ; =
        $tDate = "April " & $newDate[3] & ", " & $newDate[1]                        ; =
    ElseIf $newDate[2] = 5 Then                                                     ; =
        $tDate = "May " & $newDate[3] & ", " & $newDate[1]                          ; =
    ElseIf $newDate[2] = 6 Then                                                     ; =
        $tDate = "June " & $newDate[3] & ", " & $newDate[1]                         ; =
    ElseIf $newDate[2] = 7 Then                                                     ; =
        $tDate = "July " & $newDate[3] & ", " & $newDate[1]                         ; =
    ElseIf $newDate[2] = 8 Then                                                     ; =
        $tDate = "August " & $newDate[3] & ", " & $newDate[1]                       ; =
    ElseIf $newDate[2] = 9 Then                                                     ; =
        $tDate = "September " & $newDate[3] & ", " & $newDate[1]                    ; =
    ElseIf $newDate[2] = 10 Then                                                    ; =
        $tDate = "October " & $newDate[3] & ", " & $newDate[1]                      ; =
    ElseIf $newDate[2] = 11 Then                                                    ; =
        $tDate = "November " & $newDate[3] & ", " & $newDate[1]                     ; =
    ElseIf $newDate[2] = 12 Then                                                    ; =
        $tDate = "December " & $newDate[3] & ", " & $newDate[1]                     ; =
    Else                                                                            ; =
        MsgBox(16,"ERROR", "There was an issue analyzing the date!")                ; =
        $tDate = "ERROR"                                                            ; =
    EndIf                                                                           ; =
EndFunc                                                                             ; =
; =====================================================================================

The output would be similar to this:

Todays date is Friday October 21st, 2016.

When running the program, you will get:

First DOW : Monday October 17, 2016

I hope this helps! If you're having issues with this, please comment below and let me know what's going on. We can figure out how to get it working the way you need it to.

Thanks,

Tim

Upvotes: 0

Samoth
Samoth

Reputation: 1707

You are not looking for the function _GUICtrlMonthCal_GetFirstDOW. It will return the currently set "first column day" of your calendar component. Meaning you set it to "Monday" first and then you will always have "Sunday" returned. This just configures, which Day will be in the first column of you Month-table. Whether the top left is starting with Sunday or another day.

What you most likely want to use is _GUICtrlMonthCal_GetCurSelStr( $idMonthCal, "%02d/%02d/%04d") and then afterwards maybe do some math to resolve this to the monday of the week in which you selected the day in your month calendar.

You could use _DateAdd('d', -(_DateToDayOfWeek(@YEAR, @MON, @MDAY) - 1), _NowCalcDate()) to calculate the date of the last sunday in the current week.

Upvotes: 1

Related Questions