I. Overview
In time the page output current date into the following steps:
I, create a Date object
II, find time through the Date object
III, the specific time obtained landscaping
IV, the time displayed on the page
V, continuous display
Second, the process operation
I II, Date objects
function:
getFullYear (): Returns the current year
getMonth (): month (counting from zero)
getDate(): 日
getDay (): Week (zero-count)
getHours (): h
getMinutes (): min
getSeconds (): s
III, time beautify meaning:
When the month, the value of days, hours, minutes, less than ten seconds, it is necessary to add zero (ternary operator) on the front
In the week when 1 can not appear, the better should be "a";
IV, displayed on the page
Certainly not appear directly in an HTML element a JavaScript function, this is not preferred - it should be used a known object in JavaScript: document
Wherein the function getElementById (ID element) .innerHTML, content time required input from the input element
V, continuous display
Using the system function: setInterval (function, milliseconds)
Third, the code:
</script>
function p(){
var time=new Date();
var year=time.getFullYear();
var month=time.getMonth()+1;
month=month<10?"0"+month:month;
var week="日一二三四五六".charAt(time.getDay());
var day=time.getDate();
day=day<10?"0"+day:day;
var hour=time.getHours();
hour=hour<10?"0"+hour:hour;
var minute=time.getMinutes();
minute=minute<10?"0"+minute:minute;
var second=time.getSeconds();
second=second<10?"0"+second:second;
var current=year+"-"+month+"-"+day+" 星期"+week+" "+hour+":"+minute+":"+second;
document.getElementById("time").innerHTML=current;
}
p();
setInterval('p()',1000);
</script>
<span id="time"><\span>
Code for reference only, hee hee