Advertisement
smeech

Date offsets v2

Jun 16th, 2024 (edited)
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 3.47 KB | None | 0 0
  1. # Espanso snippets to return offset date from today
  2. # It was pointed out that my v1 didn't allow for different length months and years.
  3. # Thanks to @StewCam on Discord https://discord.com/channels/884163483409731584/1013914627886817372/1251704970080944189
  4. # Here are three updated versions which do.
  5. # Pick one and type e.g.: +2w, -3m etc.
  6. # My tests (Linux) indicate that in speed, Bash > Python > Node > PowerShell
  7.  
  8. # Bash version
  9.   - regex: (?P<offset>[+-]\d+)(?P<unit>[dwmy])
  10.     replace: "{{output}}"
  11.     label: Bash offset date
  12.     vars:
  13.       - name: output
  14.         type: shell
  15.         params:
  16.           shell: bash
  17.           cmd: |          
  18.             case {{unit}} in
  19.                 d) date=$(date -d "{{offset}} days" +"%d/%m/%y") ;;
  20.                 w) date=$(date -d "({{offset}} * 7) days" +"%d/%m/%y") ;;
  21.                 m) date=$(date -d "{{offset}} months" +"%d/%m/%y") ;;
  22.                 y) date=$(date -d "{{offset}} years" +"%d/%m/%y") ;;
  23.             esac
  24.             echo "$date"
  25.  
  26. # PowerShell version
  27.   - regex: (?P<offset>[+-]\d+)(?P<unit>[dwmy])
  28.     replace: "{{output}}"
  29.     label: PowerShell offset date
  30.     vars:
  31.       - name: output
  32.         type: shell
  33.         params:
  34.           shell: pwsh # Remove or comment out this line for Windows
  35.           cmd: |
  36.            switch ("{{unit}}") {
  37.                 "d" { $date = (Get-Date).AddDays({{offset}}).ToString("dd/MM/yy") }
  38.                 "w" { $date = (Get-Date).AddDays({{offset}} * 7).ToString("dd/MM/yy") }
  39.                 "m" { $date = (Get-Date).AddMonths({{offset}}).ToString("dd/MM/yy") }
  40.                 "y" { $date = (Get-Date).AddYears({{offset}}).ToString("dd/MM/yy") }
  41.             }
  42.             Write-Output $date
  43.  
  44. # Python version
  45.   - regex: (?P<offset>[+-]\d+)(?P<unit>[dwmy])
  46.     replace: "{{output}}"
  47.     label: Python offset date
  48.     vars:
  49.       - name: output
  50.         type: script
  51.         params:
  52.           args:
  53.            - python
  54.             - -c
  55.             - |
  56.              from datetime import datetime, timedelta
  57.               from dateutil.relativedelta import relativedelta
  58.               now = datetime.now()
  59.               match "{{unit}}":
  60.                   case "d": date = now + timedelta(days={{offset}})
  61.                   case "w": date = now + timedelta(weeks={{offset}})
  62.                   case "m": date = now + relativedelta(months={{offset}})
  63.                   case "y": date = now + relativedelta(years={{offset}})
  64.               print(date.strftime("%d/%m/%y"))
  65.  
  66. # Javascript version
  67.   - regex: (?P<offset>[+-]\d+)(?P<unit>[dwmy])
  68.     replace: "{{output}}"
  69.     label: Node.js offset date
  70.     vars:
  71.       - name: output
  72.         type: script
  73.         params:
  74.           args:
  75.            - node
  76.             - -e
  77.             - |
  78.              let now = new Date();          
  79.               switch('{{unit}}') {
  80.                 case 'd': date = new Date(now.setDate(now.getDate() + {{offset}})); break;
  81.                 case 'w': date = new Date(now.setDate(now.getDate() + ({{offset}} * 7))); break;
  82.                 case 'm': date = new Date(now.setMonth(now.getMonth() + {{offset}})); break;
  83.                 case 'y': date = new Date(now.setFullYear(now.getFullYear() + {{offset}})); break;
  84.               }
  85.               const day = ("0" + date.getDate()).slice(-2);
  86.               const month = ("0" + (date.getMonth() + 1)).slice(-2);
  87.               const year = date.getFullYear().toString().slice(-2);
  88.               console.log(`${day}/${month}/${year}`);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement