Convert Date Format - Missing Leading Zero's

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

Convert Date Format - Missing Leading Zero's

Post 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.
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: Convert Date Format - Missing Leading Zero's

Post 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.
stdougal
Newbie
Posts: 8
Joined: Wed Oct 14, 2015 3:16 pm

Re: Convert Date Format - Missing Leading Zero's

Post by stdougal »

Hi Padawan

Thank you very much - Yes that worked.

Regards
Post Reply