Click Here!

Saturday, 10 September 2016

Display Date and Time in Javascript




The script is simple, we will use the Javascript Object Date to get the date and the time. We will display the month in words(January, February...).

This is the code:

date_time.js

function date_time(id)
{

    date = new Date;
    year = date.getFullYear();
    month = date.getMonth();
    months = new Array('January', 'February', 'March', 'April',
                       'May', 'June', 'Jully', 'August',
                      'September','October', 'November', 'December');
    d = date.getDate();
    day = date.getDay();
    days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday',
                           'Thursday', 'Friday', 'Saturday');
    h = date.getHours();
      if(h<10)
        {
                h = "0"+h;
        }
        m = date.getMinutes();
        if(m<10)
        {
                m = "0"+m;
        }
        s = date.getSeconds();
        if(s<10)
        {
                s = "0"+s;
        }
      result = ''+days[day]+' '+months[month]+' '+d+'
                 '+year+' '+h+':'+m+':'+s;
        document.getElementById(id).innerHTML = result;
        setTimeout('date_time("'+id+'");','1000');
        return true;
}

date_time.html


<!DOCTYPE >
<html >
    <head>
     <meta http-equiv="Content-Type"  />
     <title>Display Date and Time in Javascript</title>
     <script type="text/javascript" src="date_time.js"></script>
    </head>
    <body>
            <span id="date_time"></span>
            <script type="text/javascript"> 
                window.onload = date_time('date_time');

</script>

    </body>
</html>


Viewed as


                                   Saturday September 10 2016 13:12:52


So this is simplest and easiest way of the simple digital watch designing through HTML and JavaScript.
If you want this coding just copy and paste and save the as above it.


No comments :

Post a Comment