Displaying current date and time using JavaScript and HTML time tag I am creating  a simple support ticket system. I thought It would be a nice little feature to show the current date and time. Since I want to show the current time I thought JavaScript would be best versus using PHP. I have looked at different questions posted by other user about the topic and created the following snippet. My question is if this is a efficient way to get and display the date and time using JavaScript date object. I also thought is would be appropriate to use the &lt;time&gt; tag. I also ask if I have used it in the correct form.

JavaScript and HTML



var monthName = new Array('January', 'Febuary', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
var hourap = new Array(12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
function showTime(){
    var dateObj = new Date();
    var day = dateObj.getDate(), month = dateObj.getMonth(), year = dateObj.getFullYear(), hour = dateObj.getHours(), minutes = (dateObj.getMinutes()&lt;=9?'0'+dateObj.getMinutes():dateObj.getMinutes());
    var string  = monthName[month]+
    ' '+day+
    ', '+year+
    '&lt;br /&gt;'+hourap[hour]+
    ':'+minutes+
    ' '+(hour&lt;=11?'am':'pm');
    var timeDiv = document.getElementById('time');
    if(timeDiv !== null) {
        timeDiv.innerHTML = string;
        timeDiv.setAttribute('datetime',year+'-'+(month+1&lt;=9?'0'+(month+1):month+1)+'-'+day+' '+hour+':'+minutes);

    };
};
setInterval(showTime,1000);
&lt;time id="time"&gt;&lt;/time&gt;




HTML output

&lt;time id="time" datetime="2015-01-30 22:00"&gt;January 30, 2015&lt;br&gt;10:00 pm&lt;/time&gt;


Any suggestions or comments would be appreciated. Please note that I wish to script it using pure JavaScript and not jQuery.
Semantic markup is less important for dynamically generated HTML than it is for server-generated HTML, but it is a laudable goal nonetheless.  If you're going to produce a datetime attribute for machine consumption, then I suggest using T as the delimiter between the date and time, as recommended by ISO 8601.

Since you frequently need to zero-pad a number, you should define a function for it.

For clarity, you should avoid excessively long lines of code.  In addition, I think it would be a good idea to define separate functions for each of the datetime formats.

I would eliminate the hourap array, since it's not that difficult to achieve the same effect programmatically.

The timeDiv variable refers to a &lt;time&gt; element, not a &lt;div&gt;, and should therefore be renamed.

The semicolon after the final if block is unusual.



var MONTH_NAME = ['January', 'Febuary', 'March', 'April', 'May', 'June',
                  'July', 'August', 'September', 'October', 'November', 'December'];
function showTime() {
    function twoDigit(n) {
        return ('0' + n).slice(-2);
    }
    function iso8601(date) {
        return date.getFullYear() +
               '-' + twoDigit(1 + date.getMonth()) +
               '-' + twoDigit(date.getDate()) +
               'T' + twoDigit(date.getHours()) +
               ':' + twoDigit(date.getMinutes());
    }
    function en_US(date) {
        var h = date.getHours() % 12;
        return MONTH_NAME[date.getMonth()] +
               ' '  + date.getDate() +
               ', ' + date.getFullYear() +
               '&lt;br /&gt;' + (h == 0 ? 12 : h) +
               ':'  + twoDigit(date.getMinutes()) +
               ' ' + (date.getHours() &lt; 12 ? 'am' : 'pm');
    }
    
    var timeEl = document.getElementById('time');
    if (timeEl !== null) {
        var now = new Date();
        timeEl.innerHTML = en_US(now);
        timeDiv.setAttribute('datetime', iso8601(now));
    }
};
setInterval(showTime, 1000);
&lt;time id="time"&gt;&lt;/time&gt;