Page 1 of 1

How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Fri Apr 13, 2018 1:26 am
by neecerp
I have a script that reads an xml
The incoming content is formatted as <OrderDate>3/20/2018 4:13:17 PM</OrderDate>
I would like to add 3 days to the Order Date to get a Due Date in the Switch Script below. :?: :?:
I keep getting errors that it is expecting a different date format like YYYY-MMM-DD


//*** Due Date ***
var due = getValue(contents, "OrderDate");
due = due.split(" ")[0];
contents = setValue(contents, "DueDate", due);
if (due != undefined) {
due = new Date( Date.parse(due) );
} else {
var order = new Date( Date.parse( getValue(contents,"OrderDate") ) );
switch (getValue(contents, "Handling")) {
case "N" :
order.setDate(order.getDate() + 2); //two days from order
//job.log(2, "two days from order");


}
due = order;
}
contents = setValue(contents, "OrderDate", due.toString().left(10));

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Fri Apr 13, 2018 4:27 pm
by cstevens
Your date isn't in an ISO 8601 timestamp format, so it's not something Switch will be able to parse by default.

You'll either need to handle it as a string and parse the day section out then add 2 using some logic, or convert your format into Date format, add the 2 days, then convert that timestamp format back to the string format you have in the XML.

You can build the Date object like this:

Code: Select all

var time = due.split(" ")[1];
var ampm = due.split(" ")[2];
var due = due.split(" ")[0];
var date = new Date( due.split('/')[2], due.split('/')[0], due.split('/')[1], time.split(':')[0],  time.split(':')[1], time.split(':')[2]);
if(ampm == "PM"){ date.setHours(date.getHours() + 12); } 
Then you can add the 2 days by using setTime and adding 2 days in milliseconds:

Code: Select all

date = date.setTime(date.getTime() + 172800000);
Then you'll need to reverse that process convert the ISO 8601 timestamp to the string format you had in your XML using:

Code: Select all

var dateString = date.toString();
dateString = dateString.replace(/-/g, '/');
dateString = dateString.replace(/T/, ' ');
Then add the AM/PM string based on the hour value.

If you try to handle this as a string then your code will need to handle all the month/day/year issues if the 2 day addition pushes you into the next month/year.

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Fri Apr 13, 2018 5:21 pm
by neecerp
Thank you,
I will give that a try.

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Fri Apr 13, 2018 6:11 pm
by jan_suhr
There is a free app that will solve the problem for you.
It's called "Date calculator"

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Tue Apr 17, 2018 12:46 am
by neecerp
Since you were so helpful would you mind if I asked another question?
How would I search for the word "vutek" and replace it with "ticket" in the same position of the pdf name referenced in this line of text?

var fileName = job.getNameProper() + ".pdf";
contents = setValue(contents, "InputFile", fileName);

Basically want to just change the pdf file name only if it contains "vutek" in the name.

Like this; N_CC_1400002352_130981_vutek.pdf
Want this; N_CC_1400002352_130981_ticket.pdf

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Tue Apr 17, 2018 5:39 pm
by cstevens
Changing the file name string is easy:

Code: Select all

var fileName = job.getName()
fileName = fileName.replace(/vutek/, "ticket");
regular expressions are very handy if you've never used them before btw.

If you're trying to change the name of the job in Switch and maintain the metadata which contains the due date etc. that gets more complicated. I'm not sure if you can change the job name or if you'll need to use something like inject lite to create a new job with the attached metadata.

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Tue Apr 17, 2018 8:32 pm
by Arthur
RegEx:
search for: ^(.*)(vutek)(.*)
replace with: \1ticket\3

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Mon Apr 30, 2018 11:22 pm
by dkelly
[quote="neecerp"]I have a script that reads an xml
The incoming content is formatted as <OrderDate>3/20/2018 4:13:17 PM</OrderDate>
I would like to add 3 days to the Order Date to get a Due Date in the Switch Script below. :?: :?:
I keep getting errors that it is expecting a different date format like YYYY-MMM-DD
[/quote="neecerp"]

Assuming the OrderDate is a string can be parsed by Date class --

Code: Select all

var due = getValue(contents, "OrderDate");
var theDate = new Date(Date.parse(due));
var millsecsInDay = 8.64e+7;
var theNewDate = new Date(theDate.getTime() + millsecsInDay*3);
s.log(-1, theNewDate.toString());

Re: How to add 2 days to Date in xml content- in Switch Scriptor

Posted: Mon Apr 30, 2018 11:56 pm
by neecerp
:D Thank you everyone. Got what I need.