Formatting the Current Date in MM/DD/YYYY HH:MM:SS Format Using JavaScript

Last Updated : 1 Sep, 2026

The current date can be formatted into MM/DD/YYYY HH:MM:SS format in JavaScript by extracting the month, date, year, hours, minutes, and seconds from a Date object.

The following approaches can be used to format the current date:

Approach 1: Using slice() Method

The slice() method can be used to ensure that the month, date, hours, minutes, and seconds are represented using two digits. The extracted values are then combined using / and : separators.

html
<body>

    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <p id="GFG_UP">
    </p>

    <button onclick="gfg_Run()">
        Click Here
    </button>

    <p id="GFG_DOWN">
    </p>

    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var date = new Date();

        el_up.innerHTML = "Click on the button to format"
            + " the date accordingly.<br>Date = " + date;

        function gfg_Run() {
            var Str =
                ("00" + (date.getMonth() + 1)).slice(-2)
                + "/" + ("00" + date.getDate()).slice(-2)
                + "/" + date.getFullYear() + " "
                + ("00" + date.getHours()).slice(-2) + ":"
                + ("00" + date.getMinutes()).slice(-2)
                + ":" + ("00" + date.getSeconds()).slice(-2);

            el_down.innerHTML = Str;
        }
    </script>

</body>

Approach 2: Using join() Method

The join() method can be used to combine the date and time components with / and : separators. A custom padding function ensures that single-digit values are converted to two-digit values.

html
<body>

    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <p id="GFG_UP">
    </p>

    <button onclick="gfg_Run()">
        Click Here
    </button>

    <p id="GFG_DOWN">
    </p>

    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var d = new Date();

        el_up.innerHTML = "Click on the button to format"
            + " the date accordingly.<br>Date = " + d;

        Number.prototype.padding = function(base, chr) {
            var len = (String(base || 10).length
                - String(this).length) + 1;

            return len > 0
                ? new Array(len).join(chr || '0') + this
                : this;
        }

        function gfg_Run() {
            var str =
                [(d.getMonth() + 1).padding(),
                d.getDate().padding(),
                d.getFullYear()].join('/')
                + ' ' +
                [d.getHours().padding(),
                d.getMinutes().padding(),
                d.getSeconds().padding()].join(':');

            el_down.innerHTML = str;
        }
    </script>

</body>
Comment