[Space-Script] Time left in day with progress bar visual

Although this is just a small space script, I thought I will share it since it may be appealing for some of us who want to make the most of our time by being constantly aware of it.

This space script creates a visual progress bar indicating the time left in the day

How it looks:
image

The script:

silverbullet.registerFunction("timeLeftInDayWithBar", () => {
  const now = Temporal.Now.zonedDateTimeISO('America/New_York');
  const endOfDay = now.add({ days: 1 }).with({ hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0 });

  const diff = now.until(endOfDay);

  const totalSecondsInDay = 24 * 60 * 60; // Total seconds in a day
  const remainingSeconds = diff.total('seconds');
  const percentageLeft = (remainingSeconds / totalSecondsInDay) * 100;

  const hours = diff.hours;
  const minutes = diff.minutes;

  const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;

  // Create the progress bar
  const progressBarLength = 20; // Number of characters in the bar
  const filledBarLength = Math.round((percentageLeft / 100) * progressBarLength);
  const filledBar = "█".repeat(filledBarLength);
  const emptyBar = "░".repeat(progressBarLength - filledBarLength);
  const progressBar = `[${filledBar}${emptyBar}]`;

  return `${formattedTime} ${progressBar} (${percentageLeft.toFixed(1)}%)`;
});

Note: you have to change the time zone inside the Temporal.Now.zonedDateTimeISO() to match yours by selecting from this list

I usually have SilverBullet open in the background as I work, and I have this script called on my index page like so, as a template:

 ✨ Today: **{{today()}}**
 🕑 Time Left: **{{timeLeftInDayWithBar()}}**

Limitation: It will only refresh when the template gets rendered again

Future Scope: Could be extended to work as a progress bar indicating time left to finish a project/meet some deadline

Disclaimer: This script was generated using a language model, I have little to no experience with Javascript

3 Likes

Sharing a similar script to show a progress bar for time left till deadline for a project/task.

This space script creates a visual progress bar indicating the time left from given start date & time to end date & time

How it looks:
image

The script:

silverbullet.registerFunction("timeLeftUntil", (startDateTime, targetDateTime) => {
  
    const timeZone = 'America/New_York'
  
    const target = Temporal.ZonedDateTime.from(`${targetDateTime}[${timeZone}]`);
    const start  = Temporal.ZonedDateTime.from(`${startDateTime}[${timeZone}]`);

    // Get the current time in the timezone
    const now = Temporal.Now.zonedDateTimeISO(timeZone);

    // Calculate the difference between now and the target time
    const diff = now.until(target);

    // Calculate the total duration in seconds
    const totalSeconds = diff.total('seconds');

    // Calculate the total duration from start to the target in seconds
    const totalDuration = target.epochSeconds - start.epochSeconds;

    // Calculate the percentage of time left
    const percentageLeft = Math.max(0, Math.min(100, (totalSeconds / totalDuration) * 100));

    // Extract hours and minutes from the difference
    const hours = diff.hours;
    const minutes = diff.minutes;

    // Format the time as HH:MM
    const formattedTime = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`;

    // Create the progress bar
    const progressBarLength = 20; // Number of characters in the bar
    const filledBarLength = Math.round((percentageLeft / 100) * progressBarLength);
    const filledBar = "█".repeat(Math.max(0, filledBarLength)); // Ensure non-negative length
    const emptyBar = "░".repeat(Math.max(0, progressBarLength - filledBarLength)); // Ensure non-negative length
    const progressBar = `[${filledBar}${emptyBar}]`;

    // Return the formatted time, progress bar, and percentage left
    return `${formattedTime} ${progressBar} (${percentageLeft.toFixed(1)}%)`;
});

This is how I call it on my index page as a template:

🕑 Time Left:
- _Project A_:   **{{timeLeftUntil("2025-01-21T16:54:00", "2025-01-23T12:00:00")}}**
- _Project B_:   **{{timeLeftUntil("2025-01-21T02:54:00", "2025-01-22T10:30:00")}}**
6 Likes

Wow. This is awesome! Thanks! :clap:

1 Like

Glad it was useful!

1 Like

It went straight to my Welcome page:

3 Likes

Nothing like a time-left reminder to keep us on our toes! Glad you liked it

1 Like

Just wanna say that I love your welcome page. The usage of stats and the top ‘buttons’ are really nice

1 Like

Would love to see the markdown for a home page like that. Looks nice!

1 Like

The markdown is not that interesting …
The driving space-script, space-style and space-config are more interesting than the markdown itself.

Here is the pure markdown without the scrips&styles and configs:

{[Create New Note|📝]} {[Quick Note|⚡]} | {[Navigate: To Page|📬]("Library/Core/Quick Notes")} {[Navigate: To Page|✅]("Tasks")} {[Navigate: To Page|🕓]("ChangeLog")} {[Navigate: To Page|🔗]("Bookmark Collection")}

{[Navigate: To Page|Space Overview]("Library/Core/Page/Space Overview")} {[Navigate: To Page|Templates]("Library/Core/Page/Template Index")} {[Search Space|Search]} {[Search Tag|Search Tags]} {[Navigate: All Pages Picker|Open Page]} {[Editor: Toggle Dark Mode|Dark/Light]}


## Stats
```template
✨ Today: **{{today()}}**
🕑 Time Left: **{{timeLeftInDayWithBar()}}**
📄 Total pages: **{{count({page select name})}}**
🖇️ Total attachments: **{{count({attachment select name})}}**
## Tags ({{count({tag select name})}})

{{#each {tag order by name asc select name}}}#{{name}} {{/each}}

Birthdays ![[Birthdays]]

Pinned Notes![[Pinned Notes]]

1 Like

Sooooo what does the backend look like :wink: lol

It would be too off-topic to post it here. I’ll do a new post later with all the things I’m using…stay tuned

1 Like

So, I turned this into a countdown for the year, cause… Idk why… Cause I did…

Had to modify the code to get it to work but, I like it.

silverbullet.registerFunction("timeLeftUntil", (startDateTime, targetDateTime) => {
    const timeZone = 'America/New_York';
    
    const target = Temporal.ZonedDateTime.from(`${targetDateTime}[${timeZone}]`);
    const start = Temporal.ZonedDateTime.from(`${startDateTime}[${timeZone}]`);
    const now = Temporal.Now.zonedDateTimeISO(timeZone);

    // Calculate difference between now and target
    const diff = now.until(target, {largestUnit: 'month'});

    // Calculate total duration from start to target
    const totalDuration = start.until(target);

    // Calculate percentage
    const totalSeconds = now.until(target).total('seconds');
    const totalProjectSeconds = start.until(target).total('seconds');
    const percentageLeft = Math.max(0, Math.min(100, (totalSeconds / totalProjectSeconds) * 100));

    // Break down time components
    const months = diff.months;
    const days = diff.days;
    const hours = diff.hours;
    const minutes = diff.minutes;

    // Format time display
    const formattedTime = [
        months > 0 ? `${months}mo` : '',
        days > 0 ? `${days}d` : '',
        hours > 0 ? `${hours}h` : '',
        minutes > 0 ? `${minutes}m` : ''
    ].filter(Boolean).join(' ') || '0m';

    // Progress bar
    const progressBarLength = 20;
    const filledBarLength = Math.round((percentageLeft / 100) * progressBarLength);
    const filledBar = "█".repeat(Math.max(0, filledBarLength)); 
    const emptyBar = "░".repeat(Math.max(0, progressBarLength - filledBarLength)); 
    const progressBar = `[${filledBar}${emptyBar}]`;

    return `${formattedTime} ${progressBar} (${percentageLeft.toFixed(1)}%)`;
});

2 Likes

Thanks! My first time adding a space-script, so cool! Really like SilverBullet so far. I’ve been a Notion user, but somehow this simplified, own my data workflow is appealing to me. Setup a MediaWiki years ago, like the .md file approach.

2 Likes