JS Date, Timer

Source: Internet
Author: User

The date of first. js

The date of JS is to use the built-in object date to operate, first create a date var date=new date (), and then you can call its API to get the month day, time division seconds and so on, this is not the focus of this article, this article focuses on how to put a string of special meaning or a number of numbers into a date type.

The most common method is the var date=new date ("2016/09/05 09:08:03");

The days before the day are separated by daily slashes, and the latter is separated by colons, and our date data is stitched together into such a string. Of course, this part of the moment can be removed, and then the date of the object will be 00 seconds of data.  That is, var date=new date ("2016/09/05"); is also possible.

Second. JS Timer

The SetInterval () method, called by window, can be omitted. There are two parameters, the first parameter is the function name, there are two ways of writing, the first is the direct function name, without parentheses, the second method is the function name, with parentheses, and also in quotation marks, as a string type parameter. The second parameter is the time interval of the timer, that is, how much time the function is called, in milliseconds. One of the first parameters we recommend using the second notation, which is enclosed in parentheses, is extended with quotation marks. So we can intuitively find that this is a function, otherwise without parentheses, we think it is a variable. Example:

<!DOCTYPE HTML><HTML>    <HeadLang= "en">        <MetaCharSet= "UTF-8">        <Body>            <DivID= "Timediv"></Div>            <Script>                varTimediv=document.getElementById ("Timediv"); functionWritetime () {varDate= NewDate (); varhours=date.gethours ()< Ten ? '0' +date.gethours (): Date.gethours (); varminutes=date.getminutes ()< Ten ? '0' +date.getminutes (): Date.getminutes (); varseconds=date.getseconds ()< Ten ? '0' +date.getseconds (): Date.getseconds (); Timediv.innerhtml=hours+ ':' +minutes+ ':' +seconds; } setinterval ("writetime ()",  +); </Script>        </Body></HTML>

The above example outputs the current time on the page every 1 seconds.

JS Date, Timer

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.