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

Post Reply
User avatar
neecerp
Member
Posts: 27
Joined: Tue Jun 05, 2012 9:21 pm

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

Post 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));
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

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

Post 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.
User avatar
neecerp
Member
Posts: 27
Joined: Tue Jun 05, 2012 9:21 pm

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

Post by neecerp »

Thank you,
I will give that a try.
jan_suhr
Advanced member
Posts: 586
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

Post by jan_suhr »

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
User avatar
neecerp
Member
Posts: 27
Joined: Tue Jun 05, 2012 9:21 pm

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

Post 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
cstevens
Member
Posts: 103
Joined: Tue Feb 12, 2013 8:42 pm

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

Post 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.
Arthur
Member
Posts: 113
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

Post by Arthur »

RegEx:
search for: ^(.*)(vutek)(.*)
replace with: \1ticket\3
dkelly
TOP CONTRIBUTOR
Posts: 658
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

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

Post 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());
User avatar
neecerp
Member
Posts: 27
Joined: Tue Jun 05, 2012 9:21 pm

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

Post by neecerp »

:D Thank you everyone. Got what I need.
Post Reply