Date Time Using Javascript

Last updated on May 26th, 2016 at 01:35 pm

Date Time Using Javascript

I am back with another tutorial on adding real-time date and time using javascript. Just copy paste this JavaScript on to your head section

You might also be interested in
Javascript Clock
Real Time Date Time Using Javascript
Javascript Clock With Date

function todays_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 = '<b>'+days[day]+' '+months[month]+' '+d+' '+year+' '+h+':'+m+':'+s+'</b>';
        document.getElementById(id).innerHTML = result;
        setTimeout('date_time("'+id+'");','1000');
        return true;
}

Next calling the above function when the website body loads

<body onLoad="todays_date_time('date-time')">

To load this we need to have a html tag with the id as ‘date-time’ like below

<span id="date-time"></span>

Thats it. You are done with the script. Just load your HTML to see it working.

Click to rate this tutorial!
[Total: 0 Average: 0]

Leave a Reply

Your email address will not be published. Required fields are marked *