Add "N" days to selected dd/mm/yyyy date

Share your favorite FastKeys commands
Post Reply
User avatar
marcelolima
Posts: 43
Joined: Apr 11th, ’22, 00:04

Post by marcelolima » Sep 2nd, ’25, 00:01

Hello, boss!

I need a script that adds a given number of days to a selected date (format dd/mm/yyyy).
After calculating, the new date should be pasted directly into the current text editor or shown so I can copy it.

How can I do this? The code I tried didn’t work (There are some Portuguese terms in the code. If this makes it difficult to understand, please let me know.)


Code: Select all

^!t::
    ClipSaved := ClipboardAll
    Clipboard := ""                          ; zera para garantir detecção
    SendInput, ^{vk43}                       ; Ctrl+C (compatível com layout ABNT2)
    ClipWait, 0.7
    if (ErrorLevel) {
        Clipboard := ClipSaved
        MsgBox, 48, Aviso Prévio, Não consegui copiar a seleção. Selecione uma data dd/mm/aaaa e tente novamente.
        return
    }

    sel := Trim(Clipboard)
    Clipboard := ClipSaved

    ; Captura data no formato dd/mm/aaaa
    if !RegExMatch(sel, "\b(\d{2})/(\d{2})/(\d{4})\b", m) {
        MsgBox, 48, Aviso Prévio, A seleção não contém uma data no formato dd/mm/aaaa.
        return
    }

    dd := m1, mm := m2, yyyy := m3

    ; Constrói timestamp para operações de data: yyyymmddHH24MISS
    ts := yyyy . mm . dd . "000000"

    ; Pede os dias de aviso prévio a somar
    InputBox, dias, Aviso Prévio, Quantos dias deseja acrescentar (apenas números)?, , 300, 150
    if (ErrorLevel)  ; cancelado
        return

    dias := Trim(dias)
    ; Validação simples: número inteiro (positivo ou zero)
    if (!RegExMatch(dias, "^\d+$")) {
        MsgBox, 48, Aviso Prévio, Valor inválido. Digite apenas números inteiros (ex.: 30).
        return
    }

    ; Soma os dias
    EnvAdd, ts, %dias%, Days  ; ts permanece em yyyymmddHH24MISS

    ; Formata de volta para dd/MM/yyyy
    FormatTime, novaData, %ts%, dd/MM/yyyy

    ; Mostra e copia para o clipboard
    Clipboard := novaData
    MsgBox, 64, Aviso Prévio,
    ( LTrim
      Data original: %dd%/%mm%/%yyyy%
      Dias somados: %dias%
      Nova data: %novaData%

      (A nova data já está no clipboard.)
    )
return
User avatar
Marko
Posts: 1906
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Sep 2nd, ’25, 18:52

I tried the script and it works as expected. What's exactly is not working?
User avatar
marcelolima
Posts: 43
Joined: Apr 11th, ’22, 00:04

Post by marcelolima » Sep 2nd, ’25, 22:06

I tried using several shortcut keys, but without success.
I was only able to execute it correctly by accessing Quick Find.

Why can't I execute the command with the shortcut keys I set?



https://docs.google.com/document/d/1XjE ... sp=sharing
User avatar
marcelolima
Posts: 43
Joined: Apr 11th, ’22, 00:04

Post by marcelolima » Sep 3rd, ’25, 00:21

I discovered the mistake. I specified an inappropriate window for the shortcut.
Sorry.
My mistake.



Code: Select all

; --- Seleciona a data e soma "n" dias (AHK v1) ---
Send, ^{vk43}
ClipWait, 0.5
DataSel := RegExReplace(Clipboard, "\s")

; valida dd/mm/aaaa
if !RegExMatch(DataSel, "^\d{2}/\d{2}/\d{4}$") {
    MsgBox, 48, Erro, Selecione uma data no formato dd/mm/aaaa.
    return
}

; quantos dias acrescentar?
InputBox, DiasAPI, Aviso Prévio, Quantos dias deseja acrescentar?
if (ErrorLevel or DiasAPI = "")
    return

; calcula (dias corridos)
NovaData := SomaDiasCorridos(DataSel, DiasAPI)

; insere no editor atual (ou troque por clipboard := NovaData)
SendInput, %NovaData%
return

; -------- FUNÇÕES (AHK v1) --------
SomaDiasCorridos(diaBR, qtd) {
    iso := toISO(diaBR)
    isoDT := iso . "000000"
    EnvAdd, isoDT, %qtd%, Days
    return fromISO(SubStr(isoDT, 1, 8))
}
toISO(d) {                ; dd/mm/aaaa -> yyyymmdd
    StringSplit, p, d, /
    return p3 . SubStr("0" p2, -1) . SubStr("0" p1, -1)
}
fromISO(iso) {            ; yyyymmdd -> dd/mm/aaaa
    y := SubStr(iso,1,4), m := SubStr(iso,5,2), d := SubStr(iso,7,2)
    return d "/" m "/" y
}
User avatar
marcelolima
Posts: 43
Joined: Apr 11th, ’22, 00:04

Post by marcelolima » Sep 3rd, ’25, 00:29

I use the script functionality from the last post, but then the fastkeys don't work right away. I need to close and reopen the program.

Is there anything I can do to avoid this?
User avatar
Marko
Posts: 1906
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Sep 3rd, ’25, 13:23

It works for me. Try Sleep, 200 instead of ClipWait and then experiment with different values.
User avatar
marcelolima
Posts: 43
Joined: Apr 11th, ’22, 00:04

Post by marcelolima » Sep 3rd, ’25, 21:14

On another computer I tested it with the editors I use the most (LibreOffice and Google Docs) and it worked perfectly. But on a different computer, after using the command above, FastKeys experiences a “delay/lag”.
Is there a specific reason why this happens?
Post Reply