Last active
June 26, 2025 19:14
-
-
Save smeech/c99ee564eb47b0357e6398a8a03dcda8 to your computer and use it in GitHub Desktop.
[Date offsets] Espanso snippets to return offset date from today - various versions. #espanso #python #powershell #bash #regex #javascript #ruby
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
# Espanso snippets to return offset date from today | |
# It was pointed out that my v1 didn't allow for different length months and years. | |
# Thanks to @StewCam on Discord https://discord.com/channels/884163483409731584/1013914627886817372/1251704970080944189 | |
# Here are five updated versions which do. | |
# Pick one and type e.g.: +2w, -3m etc. | |
# Bash version for Linux. For macOS Bash or Zsh see the discussion below | |
- regex: (?P<Offset>[+-]\d+)(?P<Unit>[dwmy]) | |
replace: "{{Output}}" | |
label: Bash Offset date | |
vars: | |
- name: Output | |
type: shell | |
params: | |
shell: bash | |
cmd: | | |
case {{Unit}} in | |
d) echo $(date -d "{{Offset}} days" +"%d/%m/%y") ;; | |
w) echo $(date -d "{{Offset}} weeks" +"%d/%m/%y") ;; | |
m) echo $(date -d "{{Offset}} months" +"%d/%m/%y") ;; | |
y) echo $(date -d "{{Offset}} years" +"%d/%m/%y") ;; | |
esac |
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
# Javascript version | |
- regex: (?P<offset>[+-]\d+)(?P<unit>[dwmy])n | |
replace: "{{output}}" | |
label: Node.js offset date | |
vars: | |
- name: output | |
type: script | |
params: | |
args: | |
- node | |
- -e | |
- | | |
let now = new Date(); | |
switch('{{unit}}') { | |
case 'd': date = new Date(now.setDate(now.getDate() + {{offset}})); break; | |
case 'w': date = new Date(now.setDate(now.getDate() + ({{offset}} * 7))); break; | |
case 'm': date = new Date(now.setMonth(now.getMonth() + {{offset}})); break; | |
case 'y': date = new Date(now.setFullYear(now.getFullYear() + {{offset}})); break; | |
} | |
const day = ("0" + date.getDate()).slice(-2); | |
const month = ("0" + (date.getMonth() + 1)).slice(-2); | |
const year = date.getFullYear().toString().slice(-2); | |
console.log(`${day}/${month}/${year}`); |
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
# PowerShell version | |
- regex: (?P<offset>[+-]\d+)(?P<unit>[dwmy]) | |
replace: "{{output}}" | |
label: PowerShell offset date | |
vars: | |
- name: output | |
type: shell | |
params: | |
shell: pwsh # Remove or comment out this line for Windows | |
cmd: | | |
switch ("{{unit}}") { | |
"d" { $date = (Get-Date).AddDays({{offset}}).ToString("dd/MM/yy") } | |
"w" { $date = (Get-Date).AddDays({{offset}} * 7).ToString("dd/MM/yy") } | |
"m" { $date = (Get-Date).AddMonths({{offset}}).ToString("dd/MM/yy") } | |
"y" { $date = (Get-Date).AddYears({{offset}}).ToString("dd/MM/yy") } | |
} | |
Write-Output $date |
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
# Python version | |
- regex: (?P<offset>[+-]\d+)(?P<unit>[dwmy]) | |
replace: "{{output}}" | |
label: Python offset date | |
vars: | |
- name: output | |
type: script | |
params: | |
args: | |
- python | |
- -c | |
- | | |
from datetime import datetime, timedelta | |
from dateutil.relativedelta import relativedelta | |
now = datetime.now() | |
match "{{unit}}": | |
case "d": date = now + timedelta(days={{offset}}) | |
case "w": date = now + timedelta(weeks={{offset}}) | |
case "m": date = now + relativedelta(months={{offset}}) | |
case "y": date = now + relativedelta(years={{offset}}) | |
print(date.strftime("%d/%m/%y")) |
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
# Ruby version | |
- regex: (?P<Offset>[+-]\d+)(?P<Unit>[dwmy]) | |
replace: "{{Output}}" | |
label: Ruby Offset date | |
vars: | |
- name: Output | |
type: script | |
params: | |
args: | |
- ruby | |
- -e | |
- | | |
require 'date' | |
offset = {{Offset}}.to_i | |
date = Date.today | |
result = case '{{Unit}}' | |
when 'd' then date + offset | |
when 'w' then date + (offset * 7) | |
when 'm' then date >> offset | |
when 'y' then date >> (offset * 12) | |
end | |
puts result.strftime('%d/%m/%y') |
Massive thanks for this @smeech.
The Bash version isn't working on macOS (I didn't test any of the others). It returns a blank string.
I fixed the formatting and included a prefix to reduce the chances of accidentally triggering the date offset calculation. I also simplified the code down as follows:
# Date offsets - return offset date from today # Type e.g.: d-18d, d+2w, d-3m, d+5y etc. - regex: d(?P<offset>[+-]\d+)(?P<unit>[dwmy]) replace: "{{output}}" label: Bash offset date vars: - name: output type: shell params: shell: bash cmd: | date=$(date -v "{{offset}}{{unit}}" +"%d/%m/%Y") echo "$date"
Thank you. It seems the -v
option is peculiar to macOS. It certainly simplifies things!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Massive thanks for this @smeech.
The Bash version isn't working on macOS (I didn't test any of the others). It returns a blank string.
I fixed the formatting and included a prefix to reduce the chances of accidentally triggering the date offset calculation. I also simplified the code down as follows: