silverbulletmd / silverbullet

The knowledge tinkerer's notebook
https://silverbullet.md
MIT License
2.59k stars 189 forks source link

keyboard short cut to insert current date and/or time? #1147

Open rhaynes74 opened 2 weeks ago

rhaynes74 commented 2 weeks ago

Hi folks, is there a builtin shortcut to insert current date and / or time?

v-shenoy commented 2 weeks ago

It would be convenient to have features similar to the natural-language-dates plugin for Obsidian, although not all that necessary.

The other alternative is to achieve this global using a text-expander such as espanso

LumenYoung commented 2 weeks ago

No builtin yet, but you can easily achieve that with the following template and space script

template:

---
tags: template
description: "Current time"
hooks.snippet.slashCommand: cc
---
{{currenttime}} 1|^|

space script:

/* get current local date and time */
silverbullet.registerFunction({name: "localtime"}, () => {
  const options = {
    timezone: 'Europe/Berlin',
    hour12: false,
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  };

  const d = new Date();
  let t = d.toLocaleString('en-US',options); /* mm/dd/yyyy, HH:MM:ss */
  t = t.replace(/,/,'');
  t = t.slice(6,10) + '-' + /* yyyy */
      t.slice(0,2) + '-' +  /* mm */
      t.slice(3,5) + ' ' +  /* dd */
      t.slice(-8);          /* HH:MM:ss */

  return t; /* yyyy-mm-dd HH:MM:ss */
})

/* get current time without date */
silverbullet.registerFunction({name: "currenttime"}, () => {
  const options = {
    timezone: 'Europe/Berlin',
    hour12: false,
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  };

  const d = new Date();
  let t = d.toLocaleString('en-US',options); /* mm/dd/yyyy, HH:MM:ss */

  return t; /* dd HH:MM:ss */
})

/* get current year */
silverbullet.registerFunction({name: "currentyear"}, () => {
  const d = new Date();
  let year = d.getFullYear(); // Gets the full year

  return year.toString(); /* yyyy */
});