Native JavaScript time and date for automatic Update _javascript tips

Source: Internet
Author: User
Tags time and date

The things that can change dynamically are always more attractive than static, even more practical, such as the time and date effect that can be changed automatically, the following is an example of how to implement this effect, code examples are as follows:
first, the specific code

 
 

The above code to achieve our requirements, the following a brief introduction to the implementation process.
Ii. Principle of realization
The time () function
can get the current time date, and then the function at the end using the timer function recursive call times () function, that is, can continue to execute the moment () function, so also realize the date of the automatic update, here is not more introduced.

Third, the relevant information supplement

JavaScript time functions

JavaScript provides a Date object for calculations of time and date. The Date object has several constructors:

1. Dateobj=new date ()//Current time

2, Dateobj=new Date (milliseconds)//distance from the starting time of January 1, 1970 of the number of milliseconds

3. Date and time represented by Dateobj=new date (datestring)//String. This string can be converted using date.parse () such as "Jannuary 1, 1998 20:13:15"

4, Dateobj=new date (year, month, day, hours, minutes, seconds, microseconds)//time value, you can not write all, do not write the default is 0

Call an object function when used, such as
Year=dateobj.getfullyear ()//Get year value

The following is a list of functions for the Date object , as shown above:

1), Get the class function:
GetDate () function--Return days (1-31)
Getday () function--Returns the number of weeks (0-6)
getFullYear () function--returns four-digit years
GetHours () function--Returns the number of 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 notation (in milliseconds)
GetYear () function--return year (real year minus 1900)

2), set the class function:
(The following functions return the number of milliseconds between the date objects from midnight January 1, 1970)
Setdate () function--set the day of the month
setFullYear () function--set year, month, and day
Sethours () function--set hours, minutes, seconds, and milliseconds
Setmilliseconds () function--set the number of milliseconds
Setminutes () function--set minutes, seconds, milliseconds
Setmonth () function--Set month, day
Setseconds () function--set the day of the month
SetTime () function--use milliseconds to set the Date object
Setyear () function--set year (real year minus 1900)

3), Transformation Display class function:
Tolocalstring () function--Returns a localized string representation
tolocaledatestring Function--Returns the localized string for the date part
tolocaletimestring function--a localized string that returns the time part

Relative to the local output, there are:

ToString ()
toDateString ()
toTimeString ()

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

4), date-resolved class functions

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

The above is about the JavaScript time and date of the details, I hope to help you learn.

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.