我如何获得一周第一天的日期?

时间:2016-10-14 16:26:21

标签: date calendar autoit

#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))

2 个答案:

答案 0 :(得分:1)

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

您最有可能想要使用的是_GUICtrlMonthCal_GetCurSelStr( $idMonthCal, "%02d/%02d/%04d"),之后可能会进行一些数学运算来解决这个问题,将其解决到您在月历中选择日期的星期一。

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

答案 1 :(得分:0)

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

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

  

analyzeDate()

这个功能将做的是计算上周一的前几天,然后得到日期。使用两个预定义的函数将以YYYY / MM / DD格式返回日期。为了获得您正在寻找的结果,我们可以将日期分为年,月和日(分别),分析月份,然后以您希望的格式设置日期。

请参阅下面的示例函数。

for (int i = 0; i < all_buttons.Length; i++) {
    int index = i ;
    if (levelScript)
        all_buttons[index].onClick.AddListener(() => Load(m_LvlStartIndex+index));
}

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

示例:

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

现在,您的完整代码看起来与此类似:

<强> Calendar.au3

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

输出类似于:

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

运行程序时,您将获得:

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

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

谢谢,