Skip to content

Instantly share code, notes, and snippets.

@RashidJorvee
Created April 24, 2020 17:54

Revisions

  1. RashidJorvee created this gist Apr 24, 2020.
    24 changes: 24 additions & 0 deletions CurrentDateWithJS.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <html>
    <body>

    <h2>Current date using JavaScript new Date()</h2>

    <p id="demo"></p>
    <p id="demo1"></p>
    <p id="demo2"></p>
    <p id="demo3"></p>

    <script>
    //Create object of Date
    var today = new Date();
    //using getDate function get the date from Date object
    document.getElementById("demo1").innerHTML = day = today.getDate().toString().padStart(2, "0");
    //using getMonth function get the month from Date object
    document.getElementById("demo2").innerHTML = month = (today.getMonth() + 1).toString().padStart(2, "0")
    //using getYear function get the year from Date object
    document.getElementById("demo3").innerHTML = year = today.getFullYear();
    //Now concatinate all those variables in create a format whatsoever you want.
    document.getElementById("demo").innerHTML = year.toString() + month.toString() + day.toString();
    </script>
    </body>
    </html>