Some time-based JavaScript judgment:

Source: Internet
Author: User

First, declare a time.
You can see that there are three basic forms.
First, directly var time = new date (); // it produces the current system time.

The second is Var time = new date (dateval); // For example, dateval = 1985/02/04 02:02:02 format; it is the specified time, which is used for processing

The third is Var time = new date (year, month, date [, hours [, minutes [, seconds [, MS]) // self-evident, the name is similar.

 

Second, let's talk about how to use it. It is generally a comparison. When there are two or more, you need to check the time span.
Assume that there are only two time selection boxes. Then, process the two time boxes,
The first type is null value judgment. Needless to say, time = ''; it's done.
The second is the comparison of the time before and after, and can be compared directly with a value greater than or less. time1 <time2; the returned value is a Boolean value.
The third is to take the time value between two periods. Take the day, hour, minute, and so on. Directly

Actually, it is quite basic. If you just list it, you should use it yourself. For advanced controls, etc.

 

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en"> <br/> <HTML> <br/> <pead> <br/> <title> new document </title> <br/> <meta name = "generator" content = "editplus"> <br/> <meta name = "author" content = ""> <br/> <meta name = "keywords" content = ""> <br/> <meta name = "Description" content = ""> </P> <p> <MCE: script Type = "text/JavaScript"> <! -- <Br/> function datedemo () {// The Value assignment time is standard. <br/> var D, S = "today's date is:"; // declare the variable. <Br/> d = new date ('2014/1/28'); // create a date object. <Br/> S + = (D. getmonth () + 1) + "/"; // obtain the month. <Br/> S + = D. getdate () + "/"; // obtain the date. <Br/> S + = D. getyear (); // obtain the year. <Br/> alert (s); // return date. <Br/>}</P> <p> function datetwo () {// use today as the standard <br/> var time = new date (); <br/> var day = time. getdate (); <br/> // alert (day); // get the day <br/> // alert (time. getmonth (); // obtain the previous month of the current month <br/> // alert (time. getmonth () + 1); // get this month. <br/> alert (time. gettime (); // obtain the time. <br/> // The gettime method returns an integer, <br/> // This integer represents the number of milliseconds between the time calculated from January 1, January 1, 1970 and the time in the date object. </P> <p >}</P> <p> function Wow () {<br/> d1 = new date ('2017/20 08:32:02 '); <br/> D2 = new date (); <br/> // alert (d1 <D2); // determine the time. <br/> var time = d2.gettime ()-d1.gettime (); // The result is a millisecond value, so all values in the future will be <br/> // The result is a week, reduce the divisor in sequence and get the corresponding comments. here, all are calculated based on decimal places. You can get the integer and process it yourself <br/> // alert (Time/1000/60/60/24/7 ); // calculate the difference between the current time and the original time. this is relatively accurate, but it is calculated in milliseconds. <br/> // minute, hour, day, and week in seconds. It's almost here. <br/> // Of course, there are still months and years. <br/> alert (d1.getfullyear ()-d2.getfullyear (); // year, this is the entire year of calculation, so there is no fractional year. <br/> // Of course, you can use the preceding formula to calculate the number of years. You can divide it by 365 or 366 at most. determine whether the year is a leap, but consider more <br/> alert (d1.getmonth () + 1) + (d1.getfullyear ()-d2.getfullyear () * 12) -(d2.getmonth () + 1); // month. <br/> // It's almost here, haha, <br/>}</P> <p> // --> </MCE: SCRIPT> </P> <p> </pead> </P> <p> <body> <br/> <Table> <br/> <tr align = "Center "> <br/> <TD align =" center "> <br/> <input type =" button "onclick =" datedemo () "Name =" button "value =" button "> <br/> </TD> <br/> </tr> <br/> <tr align =" center "> <br/> <TD align = "center"> <br/> <input type = "button" onclick = "datetwo () "Name =" button1 "value =" button1 "> <br/> </TD> <br/> </tr> <br/> <tr align =" center "> <br/> <TD align = "center"> <br/> <input type = "button" onclick = "Wow () "Name =" button1 "value =" button2 "> <br/> </TD> <br/> </tr> <br/> </table> <br/> </body> <br/> </ptml>

 

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.