Open In App

How to get current formatted date dd/mm/yyyy in Javascript and append it to an input?

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The current date in “dd/mm/yyyy” format represents the day, month, and year according to the Gregorian calendar, to obtain the current date in “dd/mm/yyyy” format using JavaScript, utilize the Date object methods to extract day, month, and year components, format them accordingly, and then append the formatted date to an input field.

Approaches

Below are the following approaches by which we can get the JavaScript formatted date dd/mm/yyyy in Javascript and append it to an input:

1. Using toLocaleDateString() method

The toLocaleDateString() method formats the date based on specified locales and options. To display dd/mm/yyyy, set the locale to ‘en-GB’. Use the valueAsDate property to assign the current date as a Date object to an input field.

Syntax:

new Date().toLocaleDateString('en-GB')

Example: In this example we displays the current date using JavaScript’s toLocaleDateString() method in “en-GB” format and sets the date input field to today’s date. The date is shown in a specific format and pre-filled in the input field.

HTML
<html>
<body>
    <p>
        Current Date is:
        <span class="currDate"></span>
    </p>
    <p>
        Please select the date to start the
        course:
        <input type="date" id="dateInput" />
    </p>

    <script type="text/javascript">
        let currDate =
            new Date().toLocaleDateString(
                "en-GB"
            );

        let inputDate = new Date();

        document.querySelector(
            ".currDate"
        ).textContent = currDate;

        document.querySelector(
            "#dateInput"
        ).valueAsDate = inputDate;
    </script>
</body>

</html>

Output:

Using-toLocaleDateString()-method

Using toLocaleDateString() method Example output

2. Splicing the string from the toLocalISOString() method

The date from the toLocalISOString() method is extracted by slicing the string, splitting it, reversing, and joining to ‘dd/mm/yyyy’ format. It’s then set to an input using valueAsDate with a new Date() object.

Syntax:

new Date().toISOString().slice(0, 10).split('-').reverse().join('/')

Example: In this example we Display current date in “dd/mm/yyyy” format using toLocaleDateString() method, and append it to an input field in a basic HTML code.

HTML
<html>
<body>
    <p>
        Current Date is:
        <span class="currDate"></span>
    </p>

    <p>
        Please select the date to start the
        course:
        <input type="date" id="dateInput" />
    </p>

    <script type="text/javascript">
        let currDate = new Date()
            .toISOString()
            .slice(0, 10)
            .split("-")
            .reverse()
            .join("/");

        let inputDate = new Date();

        document.querySelector(
            ".currDate"
        ).textContent = currDate;
        document.querySelector(
            "#dateInput"
        ).valueAsDate = inputDate;
    </script>
</body>

</html>

Output:

Splicing-the-string-from-the-toLocalISOString()-method

Splicing the string from the toLocalISOString() method Example Output

3. Using Moment.js Library

Moment.js library to obtain the current date in “dd/mm/yyyy” format and append it to an input field. Moment.js simplifies date manipulation and formatting tasks in JavaScript applications

Syntax

<script src="https://momentjs.com/downloads/moment.min.js"></script>

Example: In this example we demonstrates using Moment.js library to obtain the current date in “dd/mm/yyyy” format and append it to an input field. Moment.js simplifies date manipulation and formatting tasks in JavaScript applications.

HTML
<html>
<head>
    <script src="https://momentjs.com/downloads/moment.min.js">
    </script>
</head>
<body>
    <p>
        Current Date is:
        <span class="currDate"></span>
    </p>

    <p>
        Please select the date to start the
        course:
        <input type="date" id="dateInput" />
    </p>

    <script type="text/javascript">
        let currDate =
            moment().format("DD/MM/YYYY");
        let inputDate = moment();

        document.querySelector(
            ".currDate"
        ).textContent = currDate;
        document.querySelector(
            "#dateInput"
        ).valueAsDate = inputDate.toDate();
    </script>
</body>

</html>

Output:

Using-Momentjs-Library

Using Moment.js Library Example Output

Why Use These Methods?

  • Simplicity: These methods make it easy to work with dates without manually formatting them.
  • Localization: The toLocaleDateString() method allows you to adapt the date format to different locales, which is especially useful for international applications.
  • Efficiency: String manipulation methods like toISOString() and libraries like Moment.js simplify date manipulation, making your code more concise and maintainable.
  • Flexibility: Moment.js provides greater flexibility and additional features for more complex date operations, such as timezone conversion, relative time, and parsing dates.

Best Practices for Handling Dates in JavaScript

  • Always Validate Dates: When working with user input or external data, validate the date format to ensure it’s correct.
  • Use Libraries for Complex Operations: For complex date manipulations (like timezone conversions), use libraries such as Moment.js or Day.js.
  • Consider Browser Compatibility: While toLocaleDateString() is widely supported, older browsers may not fully support all locales. In such cases, fallback methods might be necessary.
  • Avoid Date Format Issues: If your application works with multiple locales, ensure that date formats are consistent across different parts of your app.

Conclusion

In this article, we explored multiple ways to obtain the current date in the DD/MM/YYYY format using JavaScript. From using the built-in Date object methods like toLocaleDateString() and toISOString(), to leveraging the Moment.js library, each method offers a different approach depending on your project needs.



Next Article
Article Tags :

Similar Reads