Convert Date Format - Missing Leading Zero's

Post Reply
stdougal
Newbie
Posts: 5
Joined: Wed Oct 14, 2015 3:16 pm

Convert Date Format - Missing Leading Zero's

Post by stdougal » Sun Jul 28, 2019 9:31 pm

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.

Padawan
Advanced member
Posts: 213
Joined: Mon Jun 12, 2017 8:48 pm

Re: Convert Date Format - Missing Leading Zero's

Post by Padawan » Mon Jul 29, 2019 8:50 am

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.

stdougal
Newbie
Posts: 5
Joined: Wed Oct 14, 2015 3:16 pm

Re: Convert Date Format - Missing Leading Zero's

Post by stdougal » Mon Jul 29, 2019 9:02 am

Hi Padawan

Thank you very much - Yes that worked.

Regards

Post Reply