Help with incrementing month
I have a script that updates a date helper and it doesn't seem to be working correctly anymore. I have it set to increment the month, but for some reason it's incrementing the same numbers in the year as well.
{{ now().strftime('%Y-%m-%d') | replace(now().month, now().month + 1) }}
results in 3033-03-17
Any ideas what's going on with it? Potentially just a bug?
Posted Feb 18, 2023, 4:10 AM UTC
06
Hi,
Try {{ [now().year,now().month+1,now().day] |join('-')}}
Which will add 1 month to the year but it does return a string.
That works perfect. Thanks.
To answer your actual question.
The current month is Feb with a numeric value of 2, next month is March with a numeric value of 3
The current year is 2022
Your replace is replacing all 2's in the string with 3s.
Right, but why is that incrementing all the 2s? Shouldn't the now().month + 1 just increment the month?
The code,
now().monthresolves as a number, e.g. “2” when it is February.So your code
replace(now().month, now().month+1)means “replace 2 with 2+1”. Which means “replace any instance of 2 with 3”.