首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >在AutoHotkey中添加日期或时间

在AutoHotkey中添加日期或时间
EN

Stack Overflow用户
提问于 2019-02-23 20:20:54
回答 1查看 2K关注 0票数 3

我正在寻找一个内置的ahk函数,它允许用户将天、月、年甚至时间添加到现有的一天中,从而在日数达到32的情况下将其正确转换为新的月份。我什么也没找到,所以我想出了一个小解决方案:

代码语言:javascript
代码运行次数:0
运行
复制
; 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
}

遗憾的是,这个函数并没有将循环年带入方面。你们知道更好的选择吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-24 19:41:17

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

代码语言:javascript
代码运行次数:0
运行
复制
EnvAdd, Var, Value, TimeUnits

相当于

代码语言:javascript
代码运行次数:0
运行
复制
Var += Value, TimeUnits

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

示例:

代码语言:javascript
代码运行次数:0
运行
复制
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.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54845832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档