Convert full date to dd/mm/yyyy

Hi folks
I have a mapped field that I’m receiving as a date value in integer format. Example: 30 August 1999.
I need to transform this date into: dd/mm/yyyy for another field.

After the conversion, I need to create another field that counts 20 days after what was converted. Example 30/08/1999 + 20 days : 29/09/1999

Is there any way of doing this?

I’m using this code to do this function, but it gives me an error.
What am I doing wrong?
My PReS Connect version is 2021.1.1.6675

{let months = [
“January”,
“February”,
“March”,
“April”,
“May”,
“June”,
“July”,
“August”,
“September”,
“October”,
“November”,
“December”
];

function convertDateToFormat(extendedDate) {
let parts = extendedDate.split(" of ");
let day = parts[0].trim();
let month = parts[1].trim();
let year = parts[2].trim();
let monthIndex = months.indexOf(month);
day = String(day).padStart(2, ‘0’);
year = String(year).padStart(4, ‘0’);

return ${day}/${String(monthIndex + 1).padStart(2, '0')}/${year};
}

let field = record.fields[“dateField2”];
let result = convertDateToFormat(field);

document.getElementById(“result”).innerText = result;
}

Best Regards

Hi Lazim,

I have some old code from a while ago that I reuse.

Start by extracting your OriginalDate and setting the field to type Date and format as dd M yyyy.

image

In another Extact Step add a new field set to JavaScript. (Or add another JavaScript field to your currect extract step.) Name the field accordingly and add the following code:

var aDate = data.extract(1,16,0,1,"<br />").split(" ");
var arrMonths = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var fullDate;
var pad = "00";
var m = arrMonths.indexOf(aDate[1]) + 1;
m = m.toFixed(0)
var paddedM = pad.substring(0, pad.length - m.length) + m;

fullDate = aDate[0] + "/" + paddedM + "/" + aDate[2];

*Note that I am using text data as sample data, so in the above script I’m splitting the date using space as delimiter: data.extract(1,16,0,1,"<br />").split(" "); Adjust this code based on your data mapper data type.

This should return “30/08/1999” from your sample data of “30 August 1999”.

In another field add the following JavaScript: (Note the field called OriginalData in the below code, it references the original field name from your first extraction.)

var myDate = record.fields.OriginalDate;
//Add or minus days here.
var newDate = myDate.setDate(myDate.getDate() + 20);
//Convert milliseconds to date.
var date = new Date(newDate);
date;

Set this field as:
image

You can edit “+ 20” above to add or minus days from your date.

Now in your template you can add this field and simply format it to “Long Date”

Regards,
S

The second part of @Sharne’s solution (adding a certain number of days to a date) is the right way to go.

However, the first part could be simplified greatly. JavaScript automatically coerces long date strings into valid timestamps. So for instance, the following statements all return the same, valid timestamp:

new Date("15 October 2024");
new Date("October 15, 2024");
new Date("10/15/2024");
new Date("15 oct 2024");

So if your data contains dates with any of the formats above, you can extract them to Date fields in the DataMapper:

new Date( data.extract(1,10,0,1,"").trim() );
1 Like