Created
January 27, 2025 15:02
-
-
Save fanlushuai/65b096e336353ce357c0958265268acb to your computer and use it in GitHub Desktop.
日期加减 ahk 例子
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Persistent | |
#NoEnv | |
#SingleInstance Force | |
; 快捷键配置 | |
hotkeyForAction:="^q" | |
; 创建GUI窗口 | |
Gui, Add, Text, x10 y10 w100 h20, Rule_X | |
Gui, Add, Edit, x120 y10 w50 h20 vMonthX, 1 | |
Gui, Add, Text, x175 y10 w100 h20, M | |
Gui, Add, Edit, x190 y10 w50 h20 vDateX, 1 | |
Gui, Add, Text, x245 y10 w100 h20, D | |
Gui, Add, Text, x10 y40 w100 h20, Rule_Y | |
Gui, Add, Edit, x120 y40 w50 h20 vMonthY, 1 | |
Gui, Add, Text, x175 y40 w100 h20, M | |
Gui, Add, Edit, x190 y40 w50 h20 vDateY, 1 | |
Gui, Add, Text, x245 y40 w100 h20, D | |
Gui, Add, Text, x10 y70 w100 h20, Rule_Z | |
Gui, Add, Edit, x120 y70 w50 h20 vMonthZ, 1 | |
Gui, Add, Text, x175 y70 w100 h20, M | |
Gui, Add, Edit, x190 y70 w50 h20 vDateZ, 1 | |
Gui, Add, Text, x245 y70 w100 h20, D | |
Gui, Add, Text, x10 y100 w100 h20, Rule_W(optional) | |
Gui, Add, Edit, x120 y100 w50 h20 vMonthW, | |
Gui, Add, Text, x175 y100 w100 h20, M | |
Gui, Add, Edit, x190 y100 w50 h20 vDateW, | |
Gui, Add, Text, x245 y100 w100 h20, D | |
Gui, Add, Button, x60 y130 w150 h30 gSubmit, EnableHotkey(ctrl+q) | |
Gui, Show, w300 h200, Input month and day | |
return | |
Submit: | |
; 获取输入值 | |
Gui, Submit, NoHide | |
if( MonthX=="" | |
or MonthY=="" | |
or MonthZ=="" | |
or DateX=="" | |
or DateY=="" | |
or DateZ==""){ | |
MsgBox "Please check your input" | |
Return | |
} | |
global hotkeyForAction | |
Hotkey, %hotkeyForAction%,mutilInput | |
Hotkey, %hotkeyForAction%,On | |
; 定时器。定时关闭。 | |
SetTimer,cancelHotkeyForAction , 15000, Off ; 15秒后自动取消 | |
Gui, Minimize | |
return | |
cancelHotkeyForAction: | |
global hotkeyForAction | |
Hotkey, %hotkeyForAction%, Off | |
alertMsg("canceled") | |
Return | |
; 提交按钮的逻辑 | |
mutilInput: | |
; 获取输入值 | |
Gui, Submit, NoHide | |
; 计算每对输入的日期列表 | |
datesX := CalculateDates(MonthX, DateX, "X") | |
datesY := CalculateDates(MonthY, DateY, "Y") | |
datesZ := CalculateDates(MonthZ, DateZ, "Z") | |
if (MonthW!="" and DateW!=""){ | |
datesW := CalculateDates(MonthW, DateW, "W") | |
} | |
; 去除交叉冲突。前提,x,y,z,w。的顺序是保证的,以及基本的大小关系是正确的。 | |
datesX:=removeConflicts(datesX,datesY) | |
datesY:=removeConflicts(datesY,datesZ) | |
if (MonthW!="" and DateW!=""){ | |
datesZ:=removeConflicts(datesZ,datesW) | |
} | |
; 合并 | |
mergedArray := [] | |
for i, value in datesX | |
mergedArray.Push(value) | |
for i, value in datesY | |
mergedArray.Push(value) | |
for i, value in datesZ | |
mergedArray.Push(value) | |
if (MonthW!="" and DateW!=""){ | |
for i, value in datesW | |
mergedArray.Push(value) | |
} | |
OutputDebug, % mergedArray | |
; 输出按键。 | |
; sendKeyParseDatesArray(mergedArray) ; 可以保证按键顺序。但是会被wps,的自动编号干扰。在不干扰的情况下。优先选这个 | |
sendKeyParseDatesArray2(mergedArray) | |
; 关闭热键 | |
global hotkeyForAction | |
Hotkey, %hotkeyForAction%, Off | |
return | |
alertMsg(msg){ | |
ToolTip, % msg | |
SetTimer, RemoveToolTip, -2000 | |
} | |
RemoveToolTip(){ | |
ToolTip | |
} | |
; 根据规则计算日期 | |
CalculateDates(month, date, type) | |
{ | |
dates := [] | |
if (type = "X") | |
{ | |
for i, offset in [1, 2, 3] | |
dates.Push(DateAddDays(month, date, offset)) | |
} | |
else if (type = "Y") | |
{ | |
for i, offset in [-1, 0, 1, 2, 3, 6, 9] | |
dates.Push(DateAddDays(month, date, offset)) | |
} | |
else if (type = "Z") | |
{ | |
dates.Push(DateAddDays(month, date, -1)) | |
} | |
else if (type = "W") | |
{ | |
for i, offset in [-1, 0, 1, 2, 3, 6, 9] | |
dates.Push(DateAddDays(month, date, offset)) | |
} | |
return dates | |
} | |
; 可以保证按键顺序。但是会被wps,的自动编号干扰。在不干扰的情况下。优先选这个 | |
sendKeyParseDatesArray(dateArray){ | |
keyStr:="" | |
for i, date in dateArray{ | |
month := SubStr(date, 5, 2) | |
day := SubStr(date, 7, 2) | |
if month<10 | |
month:=SubStr(month,2) | |
if day<10 | |
day:=SubStr(day,2) | |
OutputDebug, % month . ". " . day "`n" | |
keyStr:=keyStr . month . ". " . day "{Enter}" | |
} | |
OutputDebug, % keyStr | |
Send, % keyStr | |
} | |
sendKeyParseDatesArray2(dateArray){ | |
for i, date in dateArray{ | |
month := SubStr(date, 5, 2) | |
day := SubStr(date, 7, 2) | |
if month<10 | |
month:=SubStr(month,2) | |
if day<10 | |
day:=SubStr(day,2) | |
OutputDebug, % month . ". " . day "`n" | |
SendRaw, % month . ". " . day | |
Send, {enter} | |
} | |
} | |
removeConflicts(datesBefore,datesAfter){ | |
finalDates:=[] | |
for i, date1 in datesBefore | |
{ | |
conflicts:=False | |
for j, date2 in datesAfter | |
{ | |
d:=date1 | |
EnvSub, d, %date2%, days | |
if (d >= 0) | |
{ | |
conflicts:=True | |
Break | |
} | |
} | |
if !conflicts | |
finalDates.Push(date1) | |
} | |
Return finalDates | |
} | |
DateAddDays(month, date, offset) | |
{ | |
if month<10 | |
month:="0" . month | |
if date<10 | |
date:="0" . date | |
; 拼接成日期格式的子串。 | |
; 2025 01 27 00 48 37 | |
; 2025 01 27 00 00 00 | |
inputDate := A_Year . month . date . "000000" | |
; 使用EnvAdd函数来增减天数 | |
; EnvAdd支持的日期输入,为:上面的字串 | |
EnvAdd, inputDate, %offset%, Days | |
FormatTime, formattedDate, %inputDate%, yyyyMMdd | |
return formattedDate | |
} | |
; GUI关闭事件 | |
GuiClose: | |
ExitApp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment