How do I Subtract no. of days from a Date Input?

Hi Team,

Is it possible to subtract number of days in a similar fashion to using +(days): to add number of days to a Date?

thank yoju

Yes, you can use the +day and use a negative number :slight_smile:

5 Likes

@emmanuel

of course you can do that! :slightly_smiling:

1 Like

Hi Emmanuel,

I have a similar issue:

I get a positive integer from an input field and now I want to subtract this integer from the current date with +year. (People type in their age and I want to calculate and save their approximate birthday).

Do you have any suggestions for me how to do this?

Thank you!

Nicola

18

3 Likes

The question you are asking is, “How do I invert the sign on a numeric value?”

The answer is: Multiply it by -1.

6 Likes

I’ve had this same problem… and I did’t think of your simple solution @keith, even though I consider myself a logically creative type :slight_smile:
I think in Bubble’s desire to make it’s product as intuitive as possible to non coders, adding -(days) etc would be a useful and not too difficult to implement solution!

1 Like

Hi Keith,

I already tried to do that, but it does not work. It seems that I cannot get the syntax right. Could you please show me exactly what I need to write.

Thank you!
Nicola

2 Likes

Yes, @nicola.kegel, I just tried this, and Bubble isn’t giving me the option to do any multiplication on the value that follows +(days):

So I want to assign the date an event’s invoice is due with the syntax:
Start Date +(days): Pay Within * -1
So if Pay Within is 14, and Start Date is 14th April, the resulting payment due date is 1st April.

But Bubble doesn’t give me the option to do multiplication following the assignment of Pay Within.

What to do? @keith, do you know how to do this?

Best wishes,
Antony.

2 Likes

Well, the expression entry field strikes again. (We kinda sorta do need “-(days)” to do this in one fell swoop.) Once your expression resolves to a date object, the expression is done.

So you have to do the negation first somewhere else.

In @nicola.kegel case, they have an input. That input will have a positive value in it (they ask the user “how old are you?” presumably meaning enter the number of years in age that you think you are).

So, they shouldn’t be using the value from that input. They should put a custom state on that input. Call it “negative years” or whatever (type number). Then they should have a “when that input is changed” workflow that simply does element > set state negative years to input years value * -1

And so their Make changes should look like:

Birthdate = Current date/time +(years): input How old are you’s negative years

You would need to do something similar.

(This is a general issue with not having parens and such in the expression builder, @emmanuel… The example above is sort of dumb, but this problem arises in more real situations where you really really really wish you just had -(interval). I have some complex date workflows where this crops up all the time and its truly a pain to go and figure out where we can do that negation precompute. Additionally, if this were an API workflow you would be well and truly effed as there is no place to do such a precompuation (though there will be if SSA performance can be improved – on of the lil SSA plug-ins I’ve built is called a “flow state” and its exactly what you’d imagine – an action that just holds a value (as a custom state does) for referencing in downstream workflow actions.)

5 Likes

Addendum: This is another argument in favor of a :negate operator that could/should be present on the boolean and number data types in Bubble. boolean:negate would return not(boolean). number:negate would return number with the opposite sign. This might be a more “Bubbly” way to do things than duplicating all of the +(interval) operators with -(interval) operators… but either would be most welcome.

Heck, if lazy, just give us :negate on all data types where the operator is a null function for data types for which it does not make sense (those types being Things, texts, dates, date ranges and date intervals).

EDIT: Strike that… this won’t actually help in this case as one would never be able to execute :negate. What we need is parens.

Thank you, this works.

Thank you for bringing this up again, Antony. I actually will report this as a bug. There should either be a -(days) or -(year) field or the option to multiply by -1 in the expression. Keith’s solution is a workaround (thanks for that), but in my opinion that’s not how things should work.

9 Likes

Is this still a thing @emmanuel ? I’m trying to calculate a date - N days using data from a database item, but there is no way to subtract days and there seems to be no way to *-1 after +(days):

1 Like

Could you share an image of your expression?? It is working fine for me…

It stops here because there is no * sign after I add my number to the +(days): modifier.

1 Like

Agreed, this has got me stumped too. Yet again I find that very, very, very straightforward things that are trivial in every other system I’ve ever encountered, are incredibly, painfully difficult in bubble. So incredible, yet so flawed.

@keith your List Popper and Friends (SSA) Plugin has saved the day for me. I had to create

  1. FLOW State Single SSA to store the zero
  2. FLOW State Single SSA to subtract the number from zero => get the negative
  3. Calculate the date+(years): the result of 2.

What should literally have been as easy as either putting a - sign in front of a value from a Thing field, or choosing the non-existant :-(years) … has taken me three hours of frustration. @emmanuel and team please take note of the important request to implement subtraction from dates.

2 Likes

I am also struggling with this. :-days/years/hours, etc would save me pain.

So I have a similar issue.

I have a situation where something should not be visible until after a # of hours has expired. So I need to subtract the hour value they enter from a certain date to figure out when to show the item.

From this thread I see that there is no “-hours” option and (at least from what I see in this thread) there is no simple syntax to multiply the value by -1 within the calculation.

So I am taking the simple, to-hell-with-performance approach…and storing both the positive number they enter AND the negative number I need for the calculation.

Efficient? Nope.

Does it work? Yep.

Do we need -hours (and -months, -years, etc.)? Yep.