Skip to main content

Return the Current Date in a Specified Format

BelowYou iscan use the following function that can be used to return the current date in a specifiedspecific format, please change accordingly.format.

The example below returns the date in the following format:

DD-MMM-YYYY

For example:

24-Aug-2026

Example

function datefunc()
 {
var m_namesmonthNames = new Array( "Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec" );
var ddate = eval('new Date()');
var curr_dateday = d.date.getDate();if(curr_date if (day < 10) {curr_date='0'+curr_date;}
var curr_monthday = d."0" + day; } var month = date.getMonth();
var curr_yearyear = d.date.getFullYear();
var finaldateformattedDate = curr_dateday + "-" + m_names[curr_month]monthNames[month]
+ "-" + curr_year;
year; return finaldate
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: MarchAugust 09,24, 20232026

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