简体   繁体   中英

Subtract days from a Date using Script Mediator in WSO2

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?

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, '-'))" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM