Enter Current time with Button inside Template

Things I have tried

I have attempted to look through the forums and online to accomplish this but so far no dice.

Hello All,
I am trying to build a button to enter a new time entry for my time log. I am currently using Buttons, Daily Notes, Templater, Natural Language Dates to get where I am today.

Below is an image showing what I have currently set up.
I have a button called “New Time Entry” that does this

name New Time Entry
type append template
action Time Entry
color green

What I’m trying to do

My Time Entry Template is doing a break at the top with some text that says “Time Here” that I currently just highlight and use the Natural Language Tool to insert the current time.
Example of my Template


Time Here

I would like for the button to append the template and place the current time where it says Time Here. Is this possible?

Any help would be appreciated, Not a huge deal but would save me some time.

That’s definitely possible with regex

var currentDate = new Date();
var current_time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
const filepath = "Path/To/Your/Time_Log_Note.md"
const file = tp.file.find_tfile(filepath) 
var content = tp.file.content
var time_regex = /Time Here\n-/g
var new_content = content.toString().replace(time_regex, current_time)
await app.vault.modify(file,new_content)

You can add this code to a templater note enclosed with “<%* code here %>”

Or you can choose to use a templater js script file that would look like this (I prefer this way since it looks cleaner and opens in vscode since it’s a .js file):

change_time.js file in templater folder

async function my_function (tp,app) {

    var currentDate = new Date();
    var current_time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
    const filepath = "Path/To/Your/Note.md"
    const file = tp.file.find_tfile(filepath) 
    var content = tp.file.content
    var time_regex = /Time Here\n-/g
    var new_content = content.toString().replace(time_regex, current_time)
    await app.vault.modify(file,new_content)

}

module.exports = my_function

Templater note that calls this function (change_time_template.md):

<% tp.change_time(tp,app) %>

Button:

```button
name Create New Insta Tweet
type append template
action Path/To/Templater/Note/change_time_template
color yellow

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