<Html xmlns = "http://www.w3.org/1999/xhtml"> <Head> <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <Title> javascript analog clock </title> <! -- Javascript sample code explanation: In this example, getFullYear of the Javascript built-in object Date is used, The getMonth and getDate methods. First declare a variable d, var d = new Date (), The date value of the current day is assigned to the variable d. Then, use getFullYear to get the year value, Use getMonth to get the month value (Note: The returned value range of getMonth is 0 to 11, So to get the actual month, add 1 ), Use getDate to get the date value of the month of the current day. In this example, "test? Statement 1: Statement 2 ", If the test condition is met, execute Statement 1, Otherwise, use Statement 2. This syntax is used for monthly and daily computing, If the value of month and day is less than 10, add 0 before the value of month and day. = --> <Script type = "text/javascript"> Function Format2Len (I ){ // If (I <10 ){ // Return "0" + I; //} // Else { // Return I; //} Return I <10? "0" + I: I; } Function RefreshClock (){ Var CurrentTime = new Date (); Var timeStr = CurrentTime. getFullYear () + "-" + Format2Len (CurrentTime. getMonth () + 1) + "-" + Format2Len (CurrentTime. getDate () + "" + Format2Len (CurrentTime. getHours () + ":" + Format2Len (CurrentTime. getMinutes () + ":" + Format2Len (CurrentTime. getSeconds ()); TxtClock. value = timeStr; } SetInterval ("RefreshClock ()", 1000 ); </Script> </Head> <Body onload = "RefreshClock ()"> <! -- Execute once when loading the page --> Current Time: <input type = "text" id = "txtClock"/> </Body> </Html> |