Date/Time Creation

I need some help with a Script Node. I am trying to create a script node that can give TWO variables to use in a string of the current Date/Time and another Date/Time in the past “X” amount of hours given by a previous entity called “LogOveride” The Date/Time format of both needs to be like this: “2022-02-24T09:00:00”

So far I have this to start but its not working:

var today = new Date();
var date = today.getFullYear()+"-"+(today.getMonth()+1)+"-""today.getDate();
var time = today.getHours()+"-"+today.getMinutes()+"-"+today.getSeconds();
var datetime = date+"T"+time;

I’m not sure how to have it create the set for the “X” hours previously and set the two variables to be used in a follow on “Service” Node. I assume the service node will look like this:

{{serviceURL}}/query/?start={{time_variable_2}}&end={{time_variable_1}}

Any help would be greatly appreciated.

Every script node runs in its own sandbox, so if you want data to be available in subsequent nodes then you must store it in the context object.

I would also recommend looking into the Moment library, which is imported by default and available through a koreUtil object. It would provide much nicer date and time formatting options.

Thank you Andy, I am very new to this so can you help me as to what my “Script” node would look like and the line for my “Display” node: Here is what I am getting from the Documentation.
//Script Node

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
context.USdate=koreUtil.intl.DateTimeFormat('en-US').format(date);

//Display Node

{{context.nameofscriptnode.USdate}}

Also at this point not sure how to use the koreUtil.moment to use the entities variable for hours to make a second date/time back that many hours from the now date.

I would generally not mix JS date objects and Moment ones.
You can create moments in many different ways:

// Entity Date1 already captured
context.date1 = koreUtil.moment().format();
context.date2 = koreUtil.moment().format("MM/DD/YYYY");
context.date3 = koreUtil.moment("2012-11-20").format();
context.date4 = koreUtil.moment(context.entities.Date1).format();
context.date5 = koreUtil.moment.utc([2012, 11, 20, 3, 0, 0]).format();
context.date6 = koreUtil.moment.tz({}, "America/Los_Angeles").format();
context.date7 = koreUtil.moment().add(3, "hours").format();

All of those will generate strings that are stored in different variables in context. Then you can reference them in another node as simply context.date1 etc. You do not include the original script block name in the reference.

As an example, date7 is one way of adding a number of hours to a moment date.

1 Like

Thank you very much for your help on this. I was able to make it work with this solution.