# 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:

```text
DD-MMM-YYYY
```

For example:

```text
24-Aug-2026
```

## Example

```javascript
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 `<span class="editor-theme-code">Date</span>` object to retrieve the current date.

- `<span class="editor-theme-code">getDate()</span>` returns the day of the month.
- `<span class="editor-theme-code">getMonth()</span>` returns the month as a zero-based value (`<span class="editor-theme-code">0</span>` for January through `<span class="editor-theme-code">11</span>` for December).
- `<span class="editor-theme-code">getFullYear()</span>` returns the four-digit year.
- The month number is used to retrieve the corresponding abbreviated month name from the `<span class="editor-theme-code">monthNames</span>` array.
- A leading zero is added to the day when necessary.

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

```text
24-Aug-2026
```

## Changing the Date Format

You can modify the `<span class="editor-theme-code">formattedDate</span>` expression to return a different format.

For example, to return `<span class="editor-theme-code">YYYY-MM-DD</span>`:

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

To return `<span class="editor-theme-code">DD/MM/YYYY</span>`:

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

> **Note:** `<span class="editor-theme-code">getMonth()</span>` returns a zero-based month value. Therefore, add `<span class="editor-theme-code">1</span>` 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>.