使用脚本中介计算 WSO2 EI 6.5.0 中两个日期("yyyy-mm-dd hh-mm-ss")之间的 Date/Time 差异

Calculate Date/Time Difference between two dates("yyyy-mm-dd hh-mm-ss") in WSO2 EI 6.5.0 using script mediator

我正在尝试使用 WSO2 EI Script Mediator 中的 JavaScript 功能来获取两个日期之间的日期时间差。

代码:

    <script language="js"><![CDATA[var log = mc.getServiceLog(); 
  log.info("Logging inside Script Mediator");  
var resolved_date= new Date("2021-07-13 10:23:04");
var current_date = new Date("2021-07-13 11:23:04");

var diff = current_date .getTime() - resolved_date.getTime();

var Hrs = diff / (1000 * 60 * 60);
log.info("Time difference::" + Hrs); 
]]></script>

但它给出如下输出

Time difference::NaN

有人可以帮忙吗?

我找到了一些解决办法。请为此添加任何其他更好的解决方案。

    <script language="js"><![CDATA[var log = mc.getServiceLog(); 
  log.info("Logging inside Script Mediator"); 
  var  simpleDateFormat = Packages.java.text.SimpleDateFormat;
var resolved_date="2021-07-13 09:23:04";
var current_date = "2021-07-13 11:23:04";
var fomatter = new simpleDateFormat("yyyy-MM-dd hh:mm:ss");
var d1 = fomatter.parse(resolved_date);
var d2 = fomatter.parse(current_date);
var difference_In_Time= d2.getTime() - d1.getTime();
log.info("difference_In_Time::" +difference_In_Time); 
var  difference_In_Minutes= (difference_In_Time/ (1000 * 60))% 60;
log.info("difference_In_Minutes ::" +difference_In_Time); 
var difference_In_Hours= (difference_In_Time/ (1000 * 60 * 60))% 24;
log.info("difference_In_Hours   ::" +difference_In_Time); 
]]></script>

输出:

difference_In_Time::7200000
difference_In_Minutes   ::7200000
difference_In_Hours     ::7200000