Trying to get today's moment through templater script

Hello! I’m new to the forums since I couldn’t find the right resources to help with my issue

What I’m trying to do

My end goal is to use templater to pull today’s google calendar events and merge them with the task list based on due date. I haven’t gotten to the task portion yet. I’m still trying to get the “today” working properly.

Plugins: Templater and Google Calendar(Yuki Gasai)

Things I have tried

If I manually set moment("2023-03-18") then I dont have a problem. However, when I try const today = moment().startOf('day'); I eventually get a “t.startOf is not a function” error. I see a few examples of people trying to get the day off of the note title but I wanted to make a kind of dashboard.

my tentative code looks like:

const {getEvents} =["google-calendar"].api;
//const moment = require('moment');
const moment = window.moment

const today = moment().startOf('day');
const tomorrow = moment(today).add(1, 'day');
//return today.format();
//return tomorrow.format();

const events = await getEvents({startDate: today.format("YYYY-MM-DD"), endDate: tomorrow.format("YYYY-MM-DD") });

The comments are additional things I’ve tried. The require was trying to force using moment but it couldn’t find it…

To confirm that my variables today and tomorrow worked, I just returned it to see if it would pass the right value. Perhaps a type problem? But what would I cast it to?

the error from console is:

Templater Error: Template parsing error, aborting. 
 t.startOf is not a function

Commenting out the last line const events = await getEvents... makes the error go away.

Please let me know what I’m doing wrong…

You’re hinting at it here, that the error is actually within the getEvents() method, and it’s most likely happening because when you do today.format("YYYY-MM-DD") you change a date variable, today, into a text. The text variable doesn’t have that function associated with it. So try something like:

const {getEvents} =["google-calendar"].api;

const today = moment().startOf('day');
const tomorrow = moment(today).add(1, 'day');

const events = await getEvents({startDate: today, endDate: tomorrow});

Now you pass the date straight through to the getEvents() method. Note that I’ve not tested this, as I’ve not installed that plugin.

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