空手道框架-为什么Javascript函数返回具有NaN值的数组? [英] Karate Framework - Why Javascript function returns an array with NaN values?

查看:58
本文介绍了空手道框架-为什么Javascript函数返回具有NaN值的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在空手道场景中编写了javascript函数,该函数将参数中的当前日期作为参数,并获取日期,年份,月份并将它们添加到数组中.但是由于某些未知原因,我得到了NaN值.请在下面查看我一直在使用的空手道步骤.

I wrote javascript function in Karate scenario and the function takes in current date in argument and gets date, year, month and adds them in a array. But for some unknown reason I get NaN values. Please see below karate steps that I have been using.

    * def dateArr2 = []
    * def dateParse =
        """
            function(myOrderDate)
            {
              dateArr2.add(myOrderDate); // this is for test purpose

              var today = new Date(myOrderDate);
              var dd = today.getDate();     
              var mm = today.getMonth()+1; //January is 0!
              var yyyy = today.getFullYear();
              dateArr2.add(yyyy);
              dateArr2.add(mm);
              dateArr2.add(dd);               
            }
        """ 
   * def ongoingDateTime = "2018-10-19T11:53:39.8795965Z"
   * eval dateParse(ongoingDateTime)     

请注意,如果我在js执行环境(例如sublime-text)中执行,类似的javascript代码对我有用.

Note, the similar javascript code works for me if I am executing in js execution environment such as sublime-text.

推荐答案

请保持简单并使用Java.文档中有示例: https://github.com/intuit/karate#java-interop

Just keep it simple and use Java please. There are examples in the doc: https://github.com/intuit/karate#java-interop

如果太麻烦了,请编写Java实用程序.

If it is too troubling, write Java utilities.

* def toDate =
    """
    function(s) {
      var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
      var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
      return sdf.parse(s)           
    }
    """ 
* def raw = "2018-10-19T11:53:39.8795965Z"
* def date = toDate(raw)
* print date.day, date.month, date.year

只需查看java.util.Date的API,您现在就拥有了所有这些功能.

Just look at the API for java.util.Date and you have all of that now.

这篇关于空手道框架-为什么Javascript函数返回具有NaN值的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆