Space Script example: Get Current Local Date and Time


/* get current local date and time */
silverbullet.registerFunction({name: "localtime"}, () => {

  const options = {
    timezone: 'America/Los_Angeles',   /* replace with your local TZ */
    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 */

Nice script, thanks for sharing/

I guess the Bake result button in the code blocks is perfect for this one so the local time doesn’t change each time you refresh the query :slight_smile:

@paletochen , I actually just use a Snippet linked to slash command, command palette, and a shortcut key, so I have multiple ways to call the function to insert the date/time as a static value, not a query. The Bake feature is cool though.

1 Like

That makes sense.
I am most of the time happy with /today but I’ll take this script from you and have a slash command ready in case I needed greater detail by adding the time.