Skip to main content

Return the Current Date in a Specified Format

You can use the following function to return the current date in a specific format.

The example below returns the date in the following format:

DD-MMM-YYYY

For example:

24-Aug-2026

Example

function datefunc() {

    var monthNames = new Array(
        "Jan", "Feb", "Mar",
        "Apr", "May", "Jun",
        "Jul", "Aug", "Sep",
        "Oct", "Nov", "Dec"
    );

    var date = new Date();

    var day = date.getDate();
    if (day < 10) {
        day = "0" + day;
    }

    var month = date.getMonth();
    var year = date.getFullYear();

    var formattedDate = day + "-" + monthNames[month] + "-" + year;

    return formattedDate;
}

var currentDate = datefunc();
log(currentDate);

How It Works

The function uses the JavaScript Date object to retrieve the current date.

  • getDate() returns the day of the month.
  • getMonth() returns the month as a zero-based value (0 for January through 11 for December).
  • getFullYear() returns the four-digit year.
  • The month number is used to retrieve the corresponding abbreviated month name from the monthNames array.
  • A leading zero is added to the day when necessary.

For example, if the current date is August 24, 2026, the function returns:

24-Aug-2026

Changing the Date Format

You can modify the formattedDate expression to return a different format.

For example, to return YYYY-MM-DD:

var formattedDate = year + "-" + (month + 1) + "-" + day;

To return DD/MM/YYYY:

var formattedDate = day + "/" + (month + 1) + "/" + year;

Note: getMonth() returns a zero-based month value. Therefore, add 1 when using the numeric month directly in the formatted date.


Last updated: August 24, 2026

If you have any feedback on this topic, please contact us at documentation@appvance.ai.