Displaying current date using JavaScript

RPE can generate documents containing the current date by using Java Script code in a Script Expression value assigned to a Text element. How does Java Script get the current date? In order to obtain the current date using the Java Script language a variable of type „Date” needs to be created. The Date class has the following methods:

  • getDate() – Returns the day of the month (from 1-31)
  • getMonth() – Returns the month (from 0-11)
  • getFullYear() – Returns the year (four digits)

Make use of these methods to obtain the information you wish to display. For example if we want to display the date in the following form: “24 September, 2010”, the following steps need to be performed: First declare a variable of type Date:

var currentTime = new Date();

The day number is returned by the getDate() method of the Date object. Since we wish the day number to be prefixed by the „0” number if the day number is smaller than 10 we need to check if the returned day number is smaller than 10 and add the extra 0 as follows:

((currentTime.getDate()<10) ? "0" : "")

The above Java Script code snippet check if the getDate() methods returns a value smaller than 10 and if it does it returns the  „0” number. Else return the „” empty string. Now that we have the code snippet that generates the extra „0” character if needed, we need to add the day number :

((currentTime.getDate()<10) ? "0" : "") + currentTime.getDate()

This Java Script code snippet will return the day number of the day prefixed by the „0” char if the returned number is smaller than 10. Next we wish to display the name of the month and not the number. In order to accomplish this, first we need to create a vector of strings containing all the months from a year:

var months = new Array(‘January’,’February’,’March’,’April’,’May’,’June’,’July’,’August’,’September’,’October’,’November’,’December’);

Using this vector and the getMonth() method we can return the name of the mouth corresponding to the returned month number easily :


Finally we extract the current year using the getFullYear() method from the Date object:


Now let’s put together all the Java Script code snippets and let’s see how the final version of the code snippet looks :

var currentTime = new Date();
var months = new Array("January","February","March","April","May","June","July","August","September","October", "November","December");
((currentTime.getDate()<10) ? "0" : "")+ currentTime.getDate() + " " +  months[currentTime.getMonth()] + " , " + currentTime.getFullYear();

Set this Java Script code snippet to the value of a Text element :

  • 1. Create a Text element from the Palette View.
  • 2. Double click on it.The “Set content for Current Element”  dialog will be opened. Select the Script Expression tab.
  • 3. Insert the Java Script code snippet presented above.
  • 4. Save and run the template. Observe the output having the current  date presented in the following format :

4 Comments to “Displaying current date using JavaScript”

  1. hjjugovic says:

    I tried this code and got a script expression error: illegal character (#3) in at line number 3.

  2. Dan says:

    Sorry for the problem. The editor has changed the quote character.
    Now I’ve changed the source code format and everything should be ok. Let me know if you encounter any other problem.

  3. hjjugovic says:

    Thanks for the fix! It works if you change the “>” to “<" in the third line. Whoohoo!

Leave a Reply