Shehryar's response may not generate truly random results due to the method used - it should be multiplying by 12 for hours and 60 for minutes, rather than using the current numbers. As a result, higher hour and minute values will occur less frequently. Additionally, there is a chance of obtaining zero hours and 60 minutes due to rounding instead of flooring the values.
The structure of the HTML document and the use of the document object are well done, so I will replicate that:
<div id="timebox"></div>
<script>
function pad(number) {
//Add a leading 0 if number is less than 10
return ((number<10)?"0":"")+number.toString();
}
function randomTime() {
//Generate a random minute in a day (1440 minutes in 24 hours)
var r = Math.floor(Math.random() * 1440);
//Calculate the hour by dividing by total minutes per hour, taking floor value
//then getting remainder after dividing by 12 (13 -> 1) and adding 1 for range 1-12
var HH = pad(1 + (Math.floor(r/60) % 12));
//Get integer remainder after dividing by 60 (remove hours part)
var MM = pad(r % 60);
//Decide between AM and PM based on time
var AMPM = (r>=720) ? "PM" : "AM";
return HH + ":" + MM + " " + AMPM;
}
document.getElementById("timebox").innerHTML=randomTime();
</script>