Format today function to YYYYMMDDHHmmss

For the new daily notes, I want to generate a file name with the format YYYYMMDDHHmmss

For example, I would like to:

-suggestedName: “Journal/Day/{{today}}”

returns:

Journal/Day/20240222073635

Is it possible?

There might be an easier way to do it, but I did something similar with Space Script. I created a file at Library/Personal/Space Script/Date functions with this in it:

# Make sure the code block starts with ```space-script
silverbullet.registerFunction("currentDateTime", () => {
  const now = new Date();
  const year = now.getFullYear();
  const month = (now.getMonth() + 1).toString().padStart(2, '0');
  const day = now.getDate().toString().padStart(2, '0');
  const hours = now.getHours().toString().padStart(2, '0');
  const minutes = now.getMinutes().toString().padStart(2, '0');
  
  return `${year}-${month}-${day} ${hours}:${minutes}`;
})

and then instead of {{today}} you can use {{currentDateTime}}. The format’s different than what you want, but you can change it in the return line

1 Like

Thank you!

That’s work fine when is used in page but return undefined when is used in template, for example
suggestedName: “{{currentDateTime}}”
return undefined

Reload, work fine.
Very thanks!! :slightly_smiling_face:

I wanted my Space to greet me with “It’s day-of-week, date, time”

so I added another bit to my Scripts.md file :

#make sure the code block starts with ```space-script

silverbullet.registerFunction(“currentDay”, () => {
const now = new Date();
const locale = “en-US”
const options = { weekday: “long” };
return(new Intl.DateTimeFormat(locale, options).format(now));
// returns “Monday” – change locate = “de-DE” and you get “Montag”
})

and added this snip to the top of my index.md:

```template
**It’s {{currentDay}}, {{currentDateTime}}**
```

1 Like

FWIW here are some space scripts I added to get some things like week number, month number, etc.

silverbullet.registerFunction({name: "weekNumberFor"}, (date) => {
  const dateObj = Temporal.PlainDate.from(date);
  return dateObj.weekOfYear;
})
silverbullet.registerFunction({name: "monthNumberFor"}, (date) => {
  const dateObj = Temporal.PlainDate.from(date);
  return dateObj.month;
})

silverbullet.registerFunction({name: "monthNumber"}, () => {
  return Temporal.Now.plainDateISO().month;
})

silverbullet.registerFunction({name: 'weekNumber'},() => {
  return Temporal.Now.plainDateISO().weekOfYear;
})

silverbullet.registerFunction({name: 'weekEnd'}, () => {
  const today = Temporal.Now.plainDateISO();
  const lastDayOfWeek = today.add({days: 7 - today.dayOfWeek});
  return lastDayOfWeek.toString();
})
silverbullet.registerFunction({name: 'year'}, () => {
return Temporal.Now.plainDateISO().year;
})

silverbullet.registerFunction({name: 'journalDate'}, () => {
const date = Temporal.Now.plainDateISO();

// Extract the components
const month = String(date.month).padStart(2, '0'); // Ensures 2 digits
const day = String(date.day).padStart(2, '0'); // Ensures 2 digits
const dayOfWeek = date.dayOfWeek;

// Map the dayOfWeek to its abbreviation
const dayOfWeekMap = {
    1: 'Mon',
    2: 'Tues',
    3: 'Wed',
    4: 'Thurs',
    5: 'Fri',
    6: 'Sat',
    7: 'Sun'
};
const dayOfWeekAbbrev = dayOfWeekMap[dayOfWeek];

// Construct the formatted string
const formattedDate = `${month}-${day}-${dayOfWeekAbbrev}`;
return formattedDate;
})

#```template
**today:** {{today}}
**Week Number for "2024-08-15":** {{weekNumberFor("2024-08-15")}}
**Current Week Number:** {{weekNumber}}
**Week Start -> Week End:** {{weekStart}} -> {{weekEnd}}
**Month Number for "2024-08-15":** {{monthNumberFor("2024-08-15")}}
**Current Month Number:** {{monthNumber}}
**Date formatted for Journal:** {{journalDate}}
#```
4 Likes