Forum Discussion

doug_fossler's avatar
doug_fossler
New Contributor
8 years ago

Javascript example for date/time manipulation in script snap?

I am attempting to write javascript for a script snap to manipulate dates, times, and date/times. I am having issues getting a date field plus a separate time field parsed as a DateTime and further having issues when I need to add a duration of time to the DateTime. Are there any examples of this somewhere?

Doug

13 Replies

  • 9 days, and no simple answer, just seems like either something is broken and snaplogic is not acknowledging it or this is so simple it is not worth the time which begs the question as to why no answer either way?

  • Can you provide some more details on what you’re trying to do and what issues you’re running into?

    There’s quite a few methods on Date objects in the expression language. Is there something in particular you are looking for?

    • Is it not descriptive enough to say i’m attempting to add a time to a date in the script in order to get an example? I’ve tried several methods now to no avail. Here is one of my attempts.

      try { load(“nashorn:mozilla_compat.js”); } catch(e) { }
      importPackage(com.snaplogic.scripting.language);
      importPackage(java.util);
      importPackage(java.math);
      importPackage(org.joda.time);

      var impl = {
      input : input,
      output : output,
      error : error,
      log : log,

      execute : function () {
          this.log.info("Executing Transform Script");
      	var pid;
      	var pit;
      	var pi;
          while (this.input != null && this.input.hasNext()) {
              try {
                  var doc = this.input.next();
      			pid = doc["punchInDate"];
      			pit = doc["punchInTime"];
      			
      			pi = LocalDateTime(pid + ' ' + pit);
      			for (var key in doc) {
      				if (key != "original"){
                          wrapper.put(key, doc[key]);
                      }
                  }
      			wrapper.put("punchIn", pi);
      			this.output.write(doc, wrapper);
      		}
      		catch(err) {
                  var wrapper = new java.util.HashMap();
                  wrapper.put("errorMsg", err);
                  this.log.error(err);
                  this.error.write(wrapper);
              }
          }
      }
      

      };

      var hook = new com.snaplogic.scripting.language.ScriptHook(impl);

    • doug_fossler's avatar
      doug_fossler
      New Contributor

      punchInDate is in “yyyy-MM-dd”
      punchInTime is in “HH:mm”

      punchIn should be something like “MM/dd/yyyy HH:mm”, or “yyyy-MM-dd HH:mm”, or “MMddyyyyHHmm”, etc.

      • Supratim's avatar
        Supratim
        Contributor III

        @doug.fossler
        Use this-
        $punchInDate.toLocaleDateString(‘{“format”:“yyyy-MM-dd HH:mm”}’

        if punchInDate is coming as date field , if it’s coming as String then 1st do Date.parse($punchInDate), then use formater in mapper snap

  • My use case will not allow this to be handled in a mapper. Unless you can tell me how to edit multiple docs based on the prior edits of said docs? I am aware of array’s but they would not solve the problem.

  • An easy example (i.e. only three docs).

    [
        {
            "groupBy": {
                "resourceId": "999000641",
                "EventDate": "07/24/2017"
            },
            "group": [
                {
                    "resourceId": "999000641",
                    "punchInDate": "07/24/2017",
                    "EventDate": "07/24/2017",
                    "break": null,
                    "costCenter": "54770",
                    "timeType": "1",
                    "punchInTime": "06:53",
                    "resourceLastName": "brown",
                    "$rowCountPernull": 1,
                    "WorkedHours": "4",
                    "punchOutTime": "10:53",
                    "siteId": "500",
                    "punchOutDate": "07/24/2017",
                    "resourceFirstName": "Test",
                    "yalePayCode": "1",
                    "optPayTypeId": 1
                },
                {
                    "resourceId": "999000641",
                    "punchInDate": "07/24/2017",
                    "EventDate": "07/24/2017",
                    "break": null,
                    "costCenter": "54620",
                    "timeType": "1",
                    "punchInTime": "10:53",
                    "resourceLastName": "brown",
                    "$rowCountPernull": 2,
                    "WorkedHours": "3.5",
                    "punchOutTime": "14:23",
                    "siteId": "500",
                    "punchOutDate": "07/24/2017",
                    "resourceFirstName": "Test",
                    "yalePayCode": "1",
                    "optPayTypeId": 1
                },
                {
                    "resourceId": "999000641",
                    "punchInDate": "07/24/2017",
                    "EventDate": "07/24/2017",
                    "break": null,
                    "costCenter": "64990",
                    "timeType": "1",
                    "punchInTime": "14:23",
                    "resourceLastName": "brown",
                    "$rowCountPernull": 3,
                    "WorkedHours": "5.25",
                    "punchOutTime": "19:38",
                    "siteId": "500",
                    "punchOutDate": "07/24/2017",
                    "resourceFirstName": "Test",
                    "yalePayCode": "1",
                    "optPayTypeId": 1
                }
            ]
        }
    ]```
  • I can do what your pipeline does, the issue is when i have a sub-group of multiple docs that require editing of their fields based on prior and former docs. Unfortunately the attempt to work around the problem does not resolve the problem. It seems that it is not possible to combine two strings and create a date-time in a script snap.