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

neecerp
Member
Posts: 20
Joined: Tue Jun 05, 2012 9:21 pm

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

Postby neecerp » Fri Apr 13, 2018 1:26 am

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

cstevens
Member
Posts: 70
Joined: Tue Feb 12, 2013 8:42 pm

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

Postby cstevens » Fri Apr 13, 2018 4:27 pm

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.

neecerp
Member
Posts: 20
Joined: Tue Jun 05, 2012 9:21 pm

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

Postby neecerp » Fri Apr 13, 2018 5:21 pm

Thank you,
I will give that a try.

jan_suhr
Member
Posts: 120
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

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

Postby jan_suhr » Fri Apr 13, 2018 6:11 pm

There is a free app that will solve the problem for you.
It's called "Date calculator"
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps

neecerp
Member
Posts: 20
Joined: Tue Jun 05, 2012 9:21 pm

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

Postby neecerp » Tue Apr 17, 2018 12:46 am

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

cstevens
Member
Posts: 70
Joined: Tue Feb 12, 2013 8:42 pm

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

Postby cstevens » Tue Apr 17, 2018 5:39 pm

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.

Arthur
Member
Posts: 34
Joined: Sat Sep 09, 2017 11:58 pm
Location: Yateley, UK

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

Postby Arthur » Tue Apr 17, 2018 8:32 pm

RegEx:
search for: ^(.*)(vutek)(.*)
replace with: \1ticket\3


Return to “Scripting”



Who is online

Users browsing this forum: No registered users and 1 guest