Script to Return Date With Month In Various Formats

Description

Sometimes a customer will have a requirement to put a date on a label in a non-standard fashion. This can be accomplished using a script to break the date up into pieces, turn these pieces into strings, manipulate the strings to the various formats required, then combine the strings into the desired date format.

For example, a customer has a specific need to return the current date, but in the following format:

13 MY 24

Where 13 is the 2 digit year, MY is a two letter format describing the month, and 24 is the day of the month.

Solution

This can be accomplished by using the following procedure:

  1. Create an array in the script with the two-character month representations
  2. Associate the array to the month/date/year selection, returning the two character month choice in the required date field.
var today = new Date();
var m_names = new Array("JA", "FE", "MA", "AP", "MY", "JN", "JL", "AU", "SE", "OC", "NO", "DE");
var dd = today.getDate(); var mm = today.getMonth(); var yy = String(today.getFullYear()).substr(2,4);
label.fields.field.(@name == 'Date').@data=yy + " " + m_names[mm] + " " + dd;

Result

Using this script, the value in the Date field will be formatted as shown in the following table.

Current Date Date Field Value
May 24, 2013 13 MY 24
Dec 25, 1997 97 DE 25
June 1, 2023 23 JN 1

Related Article