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));
How to add 2 days to Date in xml content- in Switch Scriptor
Re: How to add 2 days to Date in xml content- in Switch Scriptor
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:
Then you can add the 2 days by using setTime and adding 2 days in milliseconds:
Then you'll need to reverse that process convert the ISO 8601 timestamp to the string format you had in your XML using:
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.
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); }
Code: Select all
date = date.setTime(date.getTime() + 172800000);
Code: Select all
var dateString = date.toString();
dateString = dateString.replace(/-/g, '/');
dateString = dateString.replace(/T/, ' ');
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
Thank you,
I will give that a try.
I will give that a try.
Re: How to add 2 days to Date in xml content- in Switch Scriptor
There is a free app that will solve the problem for you.
It's called "Date calculator"
It's called "Date calculator"
Re: How to add 2 days to Date in xml content- in Switch Scriptor
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
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
Changing the file name string is easy:
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.
Code: Select all
var fileName = job.getName()
fileName = fileName.replace(/vutek/, "ticket");
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
RegEx:
search for: ^(.*)(vutek)(.*)
replace with: \1ticket\3
search for: ^(.*)(vutek)(.*)
replace with: \1ticket\3
-
- 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
[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 --
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
Thank you everyone. Got what I need.