Page 1 of 1

Convert Date Format - Missing Leading Zero's

Posted: Sun Jul 28, 2019 9:31 pm
by stdougal
Hi,

I’m trying to convert a date from an XML file.
The current format is (dd/MM/yyyy) and I want to change the format to (yyyy/MM/dd)

I have made a script to do this.

var xmlDoc = new Document(job.getPath());
var orgdate = xmlDoc.evalToString("//root/row/Date");
// orgdate result is = 22/05/2019


var day = orgdate.split("/")[0];
// day result is = 22
var month = orgdate.split("/")[1];
// month result is = 05
var year = orgdate.split("/")[2];
// year result is = 2019

var date = new String (+year + "-" + +month + "-" + +day);
// date result is = 2019-5-22

The leading zero’s are now missing
Is there a way to keep the leading zero’s?

Thank you for your help.

Re: Convert Date Format - Missing Leading Zero's

Posted: Mon Jul 29, 2019 8:50 am
by Padawan
Hi,

Can you try to replace
var date = new String (+year + "-" + +month + "-" + +day);
with
var date = new String (year + "-" + month + "-" + day);
?

The extra plus signs make that Switch interprets the string as number and removes the leading zero's.

Re: Convert Date Format - Missing Leading Zero's

Posted: Mon Jul 29, 2019 9:02 am
by stdougal
Hi Padawan

Thank you very much - Yes that worked.

Regards