Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get current date ,time, year, month, weekday using JavaScript

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 374
    Comment on it

    This example will help a user to get the current time,date, day, month and the year. Here we have used JavaScript Date class and the following methods of Date class.

    1. getFullYear(); getFullYear() returns a four-digit number
    2. getMonth(); The value returned by getMonth() is an integer between 0 and 11
    3. getDay(); The value returned by getDay() is an integer corresponding to the day of the week
    4. getHours(); The value returned by getHours() is an integer between 0 and 23.
    5. getMinutes(); The value returned by getMinutes() is an integer between 0 and 59.
    6. getSeconds(); The value returned by getSeconds() is an integer between 0 and 59.
    7. getDate(); represents the day of the month.

    Now create an htmlpage index.php

    <html>
    <head>
    <title>Get Current time</title>
    <script src="datemonth.js"  type="text/javascript">
    </head>
    <body>
    <div>
    
        <div style="overflow: hidden; height: 55%; ">
            <strong><u>Current date & time   :</u></strong><span>&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="txt"></span>
            <p style=" float:left; margin-left:10px; padding-top:50px;"></p>
        </div>
    </div>
    </body>
    </html>
    

    Now create JavaScript file datemonth.js

    var now = new Date();
    var Y = now.getFullYear();
    var month = new Array();
    month[0] = "January";
    month[1] = "February";
    month[2] = "March";
    month[3] = "April";
    month[4] = "May";
    month[5] = "June";
    month[6] = "July";
    month[7] = "August";
    month[8] = "September";
    month[9] = "October";
    month[10] = "November";
    month[11] = "December";
    var M = month[now.getMonth()];
    
    var weekday = new Array(7);
    weekday[0]=  "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";
    
    var D = weekday[now.getDay()];
    var H = now.getHours();
    var Mnt = now.getMinutes();
    var S = now.getSeconds();
    
    var date = now.getDate();
    
    var time = document.getElementById("txt").innerHTML=date+'-'+M+'-'+Y+' '+D+'  '+H+':'+Mnt+':'+S;
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: