Native javascript implements automatic update of time and date, and javascript automatically updates

Source: Internet
Author: User

Native javascript implements automatic update of time and date, and javascript automatically updates

Things that can be dynamically changed are always more attractive and even more practical than static ones, such as the time and date effects that can be automatically changed, the following code example describes how to achieve this effect. The code example is as follows:
I. Specific Code

<Html> 

The above code implements our requirements. The following describes the implementation process.
II. Implementation Principle
Time () function
You can obtain the current time and date, and then use the timer function to call the time () function recursively at the end of the function, that is, you can continuously execute the time () function, as a result, the process of automatic update of time and date is realized, which will not be described here.

Iii. supplement relevant information

Javascript Time Functions

Javascript provides the Date object for time and Date calculation. The Date object has multiple constructors:

1. dateObj = new Date () // current time

2. dateObj = new Date (milliseconds) // The number of milliseconds from January 1, January 1, 1970.

3. dateObj = new Date (datestring) // string indicates the Date and time. This string can be converted using Date. parse (), for example, "Jannuary 1, 1998 20:13:15"

4. dateObj = new Date (year, month, day, hours, minutes, seconds, microseconds) // time value. You do not need to write it all. If you do not write it all, the default value is 0.

Used to call object functions, such
Year = dateObj. getFullYear (); // obtain the year value.

Below isDate object function listThe usage is as follows:

1) Obtain class functions:
GetDate () function -- returns the number of days (1-31)
GetDay () function -- returns the number of weeks (0-6)
GetFullYear () function -- returns the four-digit year.
GetHours () function -- Return hours (0-23)
GetMilliseconds () function -- returns the number of milliseconds (0-999)
GetMinutes () function -- returns the number of minutes (0-59)
GetMonth () function -- returns the number of months (0-11)
GetSeconds () function -- number of seconds returned (0-59)
GetTime () function -- returns the timestamp representation (expressed in milliseconds)
GetYear () function -- returns the year (real year minus 1900)

2) Set class functions:
(The following functions return the number of milliseconds between the date object and midnight on January 1, January 1, 1970)
SetDate () function -- set the day of the month
SetFullYear () function -- set the year, month, and day
SetHours () function-set hours, minutes, seconds, and milliseconds
SetMilliseconds () function -- set the number of milliseconds
SetMinutes () function-set minute, second, millisecond
SetMonth () function -- set month, day
SetSeconds () function -- set the day of the month
SetTime () function -- set the date object in milliseconds
SetYear () function -- set the year (real year minus 1900)

3) convert the display class function:
ToLocalString () function -- returns the localized string representation.
ToLocaleDateString function -- returns the localized string of the date part.
ToLocaleTimeString function -- returns the localized string of the time part.

Compared with local output, there are:

ToString ()
ToDateString ()
ToTimeString ()

The difference is that the former has different local language formats based on different machines, and the latter is an internal representation format.

4) date parsing functions

Date. parse () function-parses a Date string and returns the number of milliseconds between the Date and midnight on January 1, January 1, 1970.

The above is a detailed description of the javascript time and date, and I hope to help you learn it.

Articles you may be interested in:
  • Javascript formatting time and date function code revised edition
  • Summary of js formatting time and date functions
  • Mutual conversion between js time and date and millisecond
  • Js time and date formatting and encapsulation Functions
  • Conversion functions of JavaScript datetime and timestamp
  • Js obtains the summary of current date and time and other operations
  • Js method for obtaining the current system date and time
  • JS obtains time-related functions and converts timestamp to timestamp.

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.