首页 > 解决方案 > 在 AutoHotkey 中添加日期或时间

问题描述

我一直在寻找一个内置的 ahk 功能,它允许用户将天、月、年甚至时间添加到现有的一天,从而如果天数达到 32,则将其正确转换为新的月份。我没有找到任何东西,所以我想出了这个小解决方案:

; returns an array [year, month, day, hour, minute, second]
DateTimeAdd(v_a_now,yearPlus=0,monthPlus=0,dayPlus=0,hrPlus=0,minPlus=0,secPlus=0) {
daysInMonth := { 1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31, 8:31, 9:30, 10:31, 11:30, 12:31 }

; Parse data from an A_NOW type format
; If you pass your custom "A_NOW" format remember that numbers < 10 are expected to have a leading 0

day := SubStr(v_a_now,7,2) + dayPlus
month := SubStr(v_a_now,5,2) + monthPlus
year := SubStr(v_a_now,1,4) + yearPlus

hours := SubStr(v_a_now,9,2) + hrPlus
minutes := SubStr(v_a_now,11,2) + minPlus
seconds := SubStr(v_a_now,13,2) + secPlus

; Start formatting

if(seconds >= 60) {
    tadd := seconds / 60
    seconds -= Floor(tadd) * 60
    minutes += Floor(tadd)
}

if(minutes >= 60) {
    tadd := minutes / 60
    minutes -= Floor(tadd) * 60
    hours += Floor(tadd)
}

if(hours >= 24) {
    tadd := hours / 24
    hours -= Floor(tadd) * 24
    day += Floor(tadd)
}

; We have to format the month first in order to be able to format the days later on
if(month >= 13) {
    tadd := month / 12
    month -= Floor(tadd) * 12
    year += Floor(tadd)
}

; Assmuning the number of days is an absurd number like 23424 we need to go through each month and subtract the max. amount of days from that month
cond := true
while(cond) {
    ; Get the number of max. days in this current month (sadly no loop years included :< )
    max_days_in_this_month := daysInMonth[month]

    ; If the number of days i.e. 42 is great than 31 for example in January
    if(day > max_days_in_this_month) {
        ; Subtract max. allowed days in month from day
        day -= max_days_in_this_month

        ; New Year?
        if(month == 12) {
            month := 1
            year++
        } else {
            month++
        }
    } else {
        cond := false
    }
}

; Add leading zero to numbers

return_array := [year, month, day, hours, minutes, seconds]

i := 2
while(i != return_array.MaxIndex()+1) {
    thisIteration := return_array[i]

    if(thisIteration <= 9) {
        return_array[i] := "0" thisIteration
    }
    i++
}

; Done formatting

; For testing
;~ msg := return_array[1] "/" return_array[2] "/" return_array[3] " " return_array[4] ":" return_array[5] ":" return_array[6]
;~ msgbox % msg

return return_array
}

可悲的是,这个功能并没有考虑到循环年。各位大侠知道有什么更好的选择吗?

标签: autohotkey

解决方案


在https://autohotkey.com/docs/commands/EnvAdd.htm查看 EnvAdd

EnvAdd, Var, Value, TimeUnits

相当于

Var += Value, TimeUnits

EnvAdd使用参数将日期变量Var(以 YYYYMMDDHH24MISS 格式)设置为自身加上给定日期值的总和。Valuetimeunits

例子:

newDate := %A_Now% ; or whatever your starting date is 
EnvAdd, newDate, 20, days
NewDate += 11, days
MsgBox, %newDate%  ; The answer will be the date 31 days from now.

推荐阅读