cancel
Showing results for 
Search instead for 
Did you mean: 

Javascript example for date/time manipulation in script snap?

doug_fossler
New Contributor

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 13

doug_fossler
New Contributor

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?

tstack
Former Employee

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);

Supratim
Contributor III

@doug.fossler
Please provide the example of input format you have and what is the required output format