【问题标题】:How do I get the date of the first day of a week?如何获取一周的第一天的日期?
【发布时间】:2017-02-24 03:58:23
【问题描述】:
#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

这一行只返回Monday。我希望它返回Monday October 10, 2016。我怎样才能做到这一点?

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

【问题讨论】:

    标签: date calendar autoit


    【解决方案1】:

    有几种方法可以做到这一点。我会告诉你一种方法。

    为了让您的代码保持美观和整洁,我会将所有内容放入一个单独的函数中,并在需要时调用它。今天,我们将构建一个并调用它:

    分析日期()

    这个函数将做的是计算上一个星期一是多少天,然后得到日期。使用这两个预定义函数将以 YYYY/MM/DD 格式返回日期。为了得到您想要的结果,我们可以将日期拆分为年、月和日(分别),分析月份,然后以您想要的格式设置日期。

    请参阅下面的示例函数。

    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
    

    现在,您可以在调用 MemoWrite() 之前调用该函数,并在 MemoWrite() 的参数末尾添加 $tDate 变量。

    示例:

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

    现在,您的完整代码将类似于以下内容:

    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                                                                             ; =
    ; =====================================================================================
    

    输出将与此类似:

    今天是 2016 年 10 月 21 日星期五。

    运行程序会得到:

    First DOW : Monday October 17, 2016
    

    我希望这会有所帮助!如果您对此有任何疑问,请在下面发表评论,让我知道发生了什么。我们可以弄清楚如何让它按照您需要的方式工作。

    谢谢,

    提姆

    【讨论】:

      【解决方案2】:

      您不是在寻找函数_GUICtrlMonthCal_GetFirstDOW。它将返回您日历组件当前设置的“第一列日”。这意味着您首先将其设置为“星期一”,然后您将始终返回“星期日”。这只是配置,哪一天将在您的月表的第一列中。左上角是从星期日还是其他日期开始。

      您最可能想要使用的是_GUICtrlMonthCal_GetCurSelStr( $idMonthCal, "%02d/%02d/%04d"),然后可能会做一些数学运算以将其解决为您在月历中选择日期的一周中的星期一。

      您可以使用_DateAdd('d', -(_DateToDayOfWeek(@YEAR, @MON, @MDAY) - 1), _NowCalcDate()) 来计算本周最后一个星期日的日期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 2013-11-22
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 2018-07-05
        • 1970-01-01
        相关资源
        最近更新 更多