Get Previous Month and Year (Variable Expression)


I am using Variable Expression. I would like to get the function to help get previous month and year. Especially when execution is Jan , i would like to get previous month dec

Below are the current functions i am using:
currentyear = (getYear(now())).toString().slice(-2) > this will return 23
currentMonth = (“0” +getMonthOfYear(now())).slice(-2) > this will return 01
join(currentyear,currentMonth) > this will return 2301

Now i would like to get previous month & year: 2212

Hi @mmngeoh , you can get the current day also, and just do current date - current day, it should give you the last day of the last month, and then simply apply your slice() on that generated date.

Alternatively, you use the Data&Time Shift node for this. Check this thread where I used this node with the above logic:


Hello @mmngeoh
Out from Variable Expression, KNIME base node approach can do the job:



Very nice @gonhaddock , I didn’t know you could do this in the Interval “P-1M”. What does “P” mean or stand for (I can guess that it’s the current date, or is it the date that’s generated as “Starting Point” in the config above the “Ending Point”)?

EDIT: Forgot to add: What I don’t like about doing -1 Month is that there are some special cases where this might not work. This is just a theory as I’ve not tested it. What happens if you are on March 31st? What’s the result if you do March 31 - 1 month? Does it return February 28/29?

1 Like

Hello @bruno29a
P stands for Period
I am defining a window of 2 items; starting point is the system time; and the stepping period is minus 1 month P-1M.

Here there is another example from previous post stepping by negative days:

This is the good thing, KNIME handles the calendar. Things like this + integrate scripting made me love this platform.



You could skip the “P” as far as I can remember

1 Like

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