Javascript Get Current Year Example


Share:
Published By: Admin, Published On: Jul 17, 2021, Category: Javascript
Hello Friends, Now let's see example of how to use javascript get current year example. We will use how to use if get current year in javascript. Here you will learn how to use javascript get current year. This is a short guide on get current year. Let's get started with how to get current year in javascript. Here i will give you many example how you can check javascript get current year. Example: 1
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript Get Current Year Example</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h5>How to get current year in JavaScript?</h5>    
                        </div>
                        <div class="card-body">
                            <h2></h2>
                            <button onclick="stud()" class="btn btn-primary mb-2">click me</button>
                            <p id="cmp"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            function stud() {
                var dateobj = new Date();
                var dateObject = dateobj.getFullYear();
                document.getElementById("cmp").innerHTML
                    = "The Current Year is : " + dateObject;
            }
       </script>
    </body>
</html>
Output:
2021
Example: 2
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript Get Current Year Example</title>
        <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h5>Javascript Get Current Year Example</h5>
                        </div>
                        <div class="card-body">
                            <p>Click the button to display the full year of todays date.</p>
                            <button onclick="myFunction()" class="btn btn-primary mb-2">Try it</button>
                            <p id="demo"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            function myFunction() {
                var d = new Date();
                var n = d.getFullYear();
                document.getElementById("demo").innerHTML = n;
            }
        </script>
    </body>
</html>
Output:
2021

It will help you....

Share: