Parsing dates with Java Script


RPE automatically parses dates and renders them as instructed ( date formatting property in the docspec or date pattern property in the template) but it can only do so if the element’s type in the schema is a date/time type.

When the schema does not provide the required type info or when you need to parse dates introduced by user ( in a variable) you need to do it through Java Script. A snippet showing how to parse dates that are not automatically recognized by RPE and render them in a nice format.

 // replace this with your actual attribute/template variable
var vdt = my_date_attr;

try
{
// change this to match your input date format
var fmt_in  = new java.text.SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss");

// change this to produce the desired output format.  You could use an external variable to customize the pattern at runtime
var fmt_out = new java.text.SimpleDateFormat( "MMMM dd, yyyy HH:mm:ss");

// parse the input string. if errors occur they will be caught in the catch block
var dt =fmt_in.parse(vdt);

// render the date in the new format. This is the result of the expression
fmt_out.format(dt);
}
catch(err)
{
java.lang.System.out.print( err);
// return the original value
vdt;
}
Advertisements

Author: Dragos Cojocari

Architect for Rational Publishing Engine

4 thoughts on “Parsing dates with Java Script”

  1. Hi Dragos,

    is there any way to safe an attachment of a jazz artifact to which an URL exists in the file system of the machine running the RPE launcher?

    Thanks Stefan

  2. Hey Stefan,

    RPE can include certain document types in the Word/PDF output through the “INCLUDE FILE” element. You need to set the “include type” property of the element to “physical embedding”. This however only supports a limited number of formats ( such as Word documents) and will embed the content.

    If you need to download the files you need to use an external tool ( like curl – http://curl.haxx.se/) and invoke it from a JavaScript code in RPE.

    Hope this helps.

    Regards,
    Dragos

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s