Obsidian Bases If Syntax

What I’m trying to do

I’m exploring Bases and I want to play around with the formulas. For the books I read, I put in Finish-Start to give me a duration for how long it took me to read. Great.

But, the results I get when I finished it in less than a day are “a few seconds” or when it takes a month it says “a month”. I want to use the If function to change “a few seconds” into <24 hours or “a month” → 1 month.

Things I have tried

if(Finish - Start <= duration(“1 day”), “1 day”, (Finish - Start).toString)

The 1 day works but says can’t it “toString” on type duration.

I know Bases is new, but does anyone know how to make this work?

I’m not clear on why you want to make the duration a string (or I guess I’m not clear on what you’re planning to do with the string after that?) because it will still “say” the same thing. But one quick way (out of several) to use the pattern that you started is:

if((Finish - Start).days < 1, "< 24 hours", if((Finish - Start).days == 1, "1 day", Finish - Start))

A few other forum threads try to address your larger goal of getting a more precise and friendly duration display, but in essence, I think this could work for you:

if(Finish < Start, "time travel!", if(Finish == Start, "< 24 hours", 
[

(Finish - Start).years.floor() + " years", 

(Finish - (Start + duration((Finish - Start).years.floor() + "y"))).months.floor() + " months", 

(Finish - (Start + duration((Finish - Start).years.floor() + "y") + duration((Finish - (Start + duration((Finish - Start).years.floor() + "y"))).months.floor() + "M"))).days.floor() + " days"

].filter(value.slice(0,1) > 0).join(", ")
))

It’s a handful!

For a full template to display duration (that is, until the feature gets added to Bases :crossed_fingers:) see:

Great, that worked!! Thank you so much.

It wasn’t working when I mixed the string output with duration output so I was trying to turn both into a string.

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

That’s a tricky one “if” logic in Obsidian can feel more limited than you expect. I ended up going in circles until I realised I was mixing syntax styles from different plugins.