cancel
Showing results for 
Search instead for 
Did you mean: 

SAC - API - how to calculate previous month from Current month(date)?

igdulia
Participant

Hi all,

How can we calculate Previous Month from Current Date (in Story using script)?

Do we have function like ADD_MONTHS?

I use:

var cur_date = new Date(Date.now());

And I need to have variable as previous month.

Thank you.

Accepted Solutions (0)

Answers (1)

Answers (1)

N1kh1l
Active Contributor
0 Kudos

igdulia

The below can help. There are lot of other function which you can use on current_date. Just hit ctrl space

var current_date=new Date(Date.now());
var current_month = current_date.getMonth()+1; // first month is 0
var prev_month=current_month-1;
console.log("Current Date is ");
console.log(current_date);
console.log("Current Month is ");
console.log(current_month);
console.log("Previous Month is ");
console.log(prev_month);

Output:

Nikhil

igdulia
Participant
0 Kudos

Hi Nikhil,

thank you, this I know, but in this case I will need write additional code to calculate it correct:

if Current_Month < 1, then ....

Current_Year = ...

Current Month = ...

Current_CalMonth =....

And if I will nedd Current_Month - 4 monthes, it will other "If ...."

Becouse of this I thought that maybe exist AddMonth function, but I didn't find.

Thank you.

N1kh1l
Active Contributor

igdulia

Just add a hidden table and set its date dimension to be previous month/ whatever you require and unbooked. Then you can read this in script API in variable and use it further. You can use range and offset to decide how much you want to go back or forward.

Br.

Nikhil