I want to subtract 365 days(1 year) from a Date which is in the below format using a script mediator and get the resultant Date in the same format.
2022-10-10T23:12:18.948 05:30
Following is the code I've tried:
<script language="js"><![CDATA[var log = mc.getServiceLog();
log.info("Logging inside Script Mediator");
var simpleDateFormat = Packages.java.text.SimpleDateFormat;
var end_date = mc.getProperty('DATE');
var format = new simpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
var d1 = format.parse(end_date);
var start_date = new Date();
start_date = d1.getTime() - 365;
log.info("Start Date::" start_date);
]]></script>
But it prints
Start Date::1665425557181
Expected Date
2021-10-10T23:12:18.948 05:30
Or is it possible to achieve this without using Script Mediator? What else can I try apart from using a Script Mediator?
CodePudding user response:
If you just want to get the same date of the previous year you can do something like below.
<property value="2022-10-10T23:12:18.948 05:30" name="timeStamp" scope="default" type="STRING"/>
<property name="startDate" expression="concat(substring-before($ctx:timeStamp, '-')-1, '-', substring-after($ctx:timeStamp, '-'))" />