Autohotkey 在自动热键中添加到日期或时间
我正在寻找一个内置的ahk功能,允许用户添加天,月,年,甚至时间到现有的一天,从而将其正确地转换为一个新的月,如果天计数达到32。我什么也没找到,所以我想出了一个小办法:Autohotkey 在自动热键中添加到日期或时间,autohotkey,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:
; 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
}
;返回数组[年、月、日、小时、分钟、秒]
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}
;从A_NOW类型格式解析数据
;如果您通过自定义的“A_NOW”格式,请记住<10的数字应具有前导0
日:=子项(现在为7,2)+dayPlus
月份:=SubStr(v_a_now,5,2)+monthPlus
年份:=SubStr(v_a_now,1,4)+yearPlus
小时数:=SubStr(v_a_now,9,2)+hrPlus
分钟数:=SubStr(v_a_now,11,2)+minPlus
秒:=SubStr(v_a_now,13,2)+secPlus
;开始格式化
如果(秒>=60){
tadd:=秒/60
秒-=地板(tadd)*60
分钟+=楼层(tadd)
}
如果(分钟>=60){
tadd:=分钟/60
分钟-=楼层(tadd)*60
小时数+=楼层(tadd)
}
如果(小时数>=24){
tadd:=小时/24
小时数-=楼层(tadd)*24
日+=楼层(tadd)
}
;我们必须先格式化月份,以便能够在以后格式化日期
如果(月份>=13){
tadd:=月/12
月份-=楼层(tadd)*12
年份+=楼层(tadd)
}
假设天数是一个荒谬的数字,比如23424,我们需要通过每个月减去该月的最大天数
条件:=正确
while(cond){
;获取当前月份的最大天数(遗憾的是,没有包含循环年:<)
本月的最大天数:=daysInMonth[月]
;如果天数(即42天)大于31天,例如1月份
如果(本月的天数>最大天数){
;从天减去月内允许的最大天数
天-=本月的最大天数
新年?
如果(月份==12){
月份:=1
年++
}否则{
月++
}
}否则{
条件:=假
}
}
;将前导零添加到数字
return_数组:=[年、月、日、时、分、秒]
i:=2
while(i!=return_array.MaxIndex()+1){
thisIteration:=返回数组[i]
如果(thisIteration在
相当于
Var += Value, TimeUnits
EnvAdd
使用timeunits
参数将日期变量Var
(YYYYMMDDH24miss格式)设置为自身加上给定日期值值
的总和
例如:
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.
太好了!谢谢:)不客气。我当然很感谢你的努力,也许可以帮助你完成闰年部分,但是AHK日期处理完成了所有繁重的工作,所以,没有任何意义。打字?循环年份>闰年?如果你想的话,我可以编辑。
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.