How to simulate a clock using javascript

Source: Internet
Author: User

How to simulate a clock using javascript

This example describes how to use javascript to simulate a clock. Share it with you for your reference. The specific implementation method is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

<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>

I hope this article will help you design javascript programs.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.