Parse and Format Date

Hi

I have a field that’s “22-12-23” that I want to format to “22 December 2022”. Could someone point me in the direction on what code I need to use

James

When you mapped the data to a date field in the Data Mapper you could use the following script in the Designer to add the respective date in the suggested format:

let bdStr =  formatter.date( record.fields.birthday, "d MMMM Y")
results.html( bdStr )

date.OL-template (7.2 KB) date.OL-datamapper (3.2 KB)

Mapping the string to a date using a custom pattern in Data Mapper.

Use the formatter object with a custom pattern in the Designer.

In case it would be “2022-12-23” instead of “22-12-23” you could use the following JavaScript code, for example:

/** See {@link https://stackoverflow.com/a/1643468} */
var monthNames = [
    "January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"
];
var str = "2022-12-22"; //data.extract("Date", 0);
var d = new Date(str);
var result = "";

result += d.getDate() + " ";
result += monthNames[d.getMonth()] + " ";
result += d.getFullYear();

result;

Executing the following JavaScript code will unfortunately result in Invalid Date:

var d = new Date("22-12-22");

I need it to be in the metadata like that too as I need to write out an XML also for each record. However I can add that as a action after the extract I guess within the mapper then it will be correct in the metadata

In that case you could add a second field in Data Mapper and apply Marten’s suggestion. The following example reuses the extracted date for a second field:

var monthNames = [
    "January", "February", "March", "April", "May", "June", "July",
    "August", "September", "October", "November", "December"
];
var d = new Date( record.birthday )

d.getDate() + " " + monthNames[d.getMonth()] + " " + d.getFullYear();
1 Like