Insert current time incremented by amount

What I’m trying to do

I am using the templater plugin to create a list of task for my day.
My list looks like this:

Day Planner

  • <% tp.date.now(“HH:mm”) %> First task
  • <% tp.date.now(“HH:mm”,60) %> Second task <—Current time incremented by 60 minutes
  • <% tp.date.now(“HH:mm”,120) %> Third task <—Current time incremented by 120 minutes

The time for the first task is correctly inserted when I invoke the template.
For the second task I want to be able to increase the current time by 60 minutes.
Its possible to increment the current time using the templater?

According to tp.date.now() it should be possible to specify a time period as the offset, if you follow the ISO8601 standard.

Sadly, it seems like it’s still only respecting time periods related to days, and not those specific to the time part.

I tried the following variants:

<% tp.date.now("YYYY-MM-DD HH:mm") %> <br>
<% tp.date.now("YYYY-MM-DD HH:mm", "+PT1H1") %> <br>
<% tp.date.now("YYYY-MM-DD HH:mm", "+T1H30M") %> <br>
<% tp.date.now("YYYY-MM-DD HH:mm", 0.1) %> <br>
<% tp.date.now("YYYY-MM-DD HH:mm", "P1Y2M") %> <br> 

And only the last variant with adding 1 year and 2 months, where effective.

In other words, it seems like you need to switch to javascript and use moment() to get the time calculation you want. So here are some actually working variants:

<%* const plus1h45m = moment().add("PT1H45M").format("HH:mm") _%>

- 1 hour: <%* tR += moment().add(1, "h").format("HH:mm") %>
- 1 hour 30m: <%* tR += moment().add("PT1H30M").format("HH:mm") %>
- 2 hours: <%* tR += moment().add(2, "h").format("HH:mm") %>
- Precalculated:  <% plus1h45m %>

Note that all but that last of these examples uses the Execution command variants. The last one uses the calculated value from the first of these.

Is there a way somehow to add to a saved time?
Something like:

<%* const currentTime= moment().format("HH:mm") _%>

- 1 hour after current time: <%* tR += currentTime.add(1, "h").format("HH:mm") %>
- 1 hour after last value of tR or 2 hours after current time: <%* tR += tR.add(1,"h").format("HH:mm") %>
- 2 hours after last value of tR or 4 hours after current time: <%* tR += tR.add(2, "h").format("HH:mm") %>

You mean something like the following:

// If you want to fix the start time, use something like
// const startTime = moment("2000-01-02T08:00") 
// For current time, use the line below
const startTime = moment() 

function addOffset(offset) {
  return startTime.clone().add(offset).format("HH:mm")
}

tR += "- 1 hour and 5 min after current time: " + addOffset("PT1H5M") + "\n"
tR += `- 45 min after start: ${ addOffset("PT45M") }\n`
_%>
- 2 hours since start: <% addOffset("PT2H") %>
- 2.5 hours: <% addOffset("PT2H30M") %>

Here I show how to use that extra function in three different version, all using a standard ISO 8601 way of defining the time period to add to your starting time.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.