Learn about the front-end JAVASCRIPT-4, JavaScript basics Math, and date objects from scratch

Source: Internet
Author: User
Tags cos local time pow sin

Introduction to the Math object

1:math ObjectThe Math object is used to perform mathematical tasks. It is not a class of objects like Date and String, so there is no constructor for Math (). You do not have to create it, and you can invoke all of its properties and methods by using Math as an object.

2:math Property

Pi: Returns the pi (approximately equal to 3.14159).

3:math method

Math.Round (3.6); Rounded.

Math.random (); Returns a random number greater than or equal to 0 to less than 1.

Random number how to set the range//0-100 (inclusive) between the random values-math.round rounded Console.log (Math.Round (Math.random () * 100));//0-99 (inclusive) between the random values- Math.floor Console.log (Math.floor (Math.random () * 100)), and//1-100 (inclusive) random values-math.ceil up rounding Console.log (Math.ceil ( Math.random () * 100));//100-1000 (inclusive) random values between//two values/*function random (x, y) {return Math.Round (Math.random () * (Y- x)) + x;} */console.log (Math.Round (Math.random () * (1000-100)) + 100);

Math.max (A, b); Returns a larger value.

Math.min (A, b); Returns a smaller number.

Math.Abs (num); Returns the absolute value.

Math.ceil (3.6); Rounding up. 4

Math.floor (3.6); Rounding down. 3

Math.pow (x, y); The Y-side of X.

MATH.SQRT (num); Open Square.

4: Trigonometric Functions Review

Math.sin (x); The sine of x, the return value is between 1 and 1.

Math.Cos (x); The cosine of X, and the return value is between 1 and 1.

Appendix:

Pythagorean theorem

var a = 3, b = 4;var c = math.sqrt (Math.pow (A, 2) + Math.pow (b, 2)); Console.log (c);

Trigonometric function:

var degree = 60;var rad = math.pi/180 * Degree;console.log (Math.ceil (RAD) */); Math.sin (Console.log R (Math.Cos (RAD) *)/ten);

Introduction to date Dates Object

1: Date Object

Definition: The number of milliseconds that JS date uses UTC (International coordinated Time) 1970,1,1,0,0,0,0.

The date in JS is also its built-in object, so we have to get and manipulate the date, we have to instantiate the object.

2: Create Date Object

var odate=new Date ();

Will contain information about the local time, including the day of the month and the week.

The format in which parameters can be passed in:

1. "Time: minute: Second month/day/year", "month/day/years: minutes: Seconds", "Year/month/day" and other strings.

var odate = new Date (' 12:12:12 12/12/2017 '); "Time: minute: Second month/day/year" var odate = new Date (' 12/14/2017 12:12:12 '); "Month/day/year: minutes: Seconds" var odate = new Date (' 2017/12/14 '); Strings such as "Year/month/day"

2. Year, month, day, time, minute, second. Note that you cannot add "". the month is calculated starting from 0.

var odate = new Date (2017, 11, 12, 13, 24, 36);//0 for 1

3. Time stamp.

var odate = new Date (1516775284475);

3: Date object method of obtaining information

Note: Both the month and the week are calculated starting from 0.

GetDate (): Returns the day of the one month (1 ~ 31) from the Date object.

GetMonth (): Returns the month (0 ~ 11) from the Date object.

getFullYear (): Returns the year as a four-digit number from a Date object.

GetDay (): Returns the day of the week (0 ~ 6) from the Date object. 0 of them represent Sunday.

getYear () : Please use the getFullYear () method instead. the value returned by GetYear () is not always a 4 digit number! For years between 1900 and 1999, The GetYear () method returns only two digits. A 4-digit number is returned for a year after 1900 or 1999! ECMAScript is no longer required to use this function .

GetHours (): Returns the hour of the Date object (0 ~ 23).

Getminutes (): Returns the minute of the Date object (0 ~ 59).

Getseconds (): Returns the number of seconds (0 ~ 59) of the Date object.

Getmilliseconds (): Returns the milliseconds (0 ~ 999) of the Date object.

GetTime (): Returns the number of milliseconds since January 1, 1970.

4: Method of setting information for date objects

SetDate (): Sets the day of the month in the Date object (1 ~ 31).

Setmonth (): Sets the month (0 ~ 11) in the Date object.

setFullYear (): Sets the year (four digits) in the Date object.

setyear () please use the setFullYear () method instead. If the year parameter is a two-bit number, such as Setyear (91), the method will be interpreted as 1991. If you want to specify a year before 1990 or after 1999 years, use a four-digit number. ECMAScript is no longer required to use this function .

Sethours (): Sets the hour (0 ~ 23) in the Date object.

Setminutes (): Sets the minute (0 ~ 59) in the Date object.

Setseconds (): Sets the second (0 ~ 59) in the Date object.

Setmilliseconds (): Sets the milliseconds (0 ~ 999) in the Date object.

SetTime (): Sets the Date object in milliseconds.

5: Common operations on date objects

1: Formats the date into a string.

2: Converts the specified format string into a Date object.

3: The date string is converted to milliseconds.

4: Calculates a time difference of two dates.

5: Date function Encapsulation (dateutil.js) (encapsulation common functions)

6: Delay Timer and timer

Delay Device:

Syntax: setTimeout (function or code string, specified time (ms));

Executes the function or code only once after the specified number of milliseconds.

Purge delay: cleartimeout ();

var timer3 = setTimeout (function () {Console.log (' Jumping! ‘);} SetTimeout (function () {cleartimeout (TIMER3);}, 2000);

SetTimeout () The way the function is called, or the quotation marks can be removed

function print () {console.log (' hello ');} SetTimeout (' Print () ', 3000);//This writing if in the window.onload call, proposed to put the anonymous function,//related to the scope of the problem, JS run to this error, can also adjust the location of JS reference

  

  Timer:

Syntax: setinterval (function or code string, specified time (ms));

Executes a function or a string of code in a specified period (in milliseconds).

Clear Timer: clearinterval ();

var num = 6;var timer = setinterval (function () {console.log (--num); if (num = = 0) {clearinterval (timer);}}, 1000);

  

Learn about the front-end JAVASCRIPT-4, JavaScript basics Math, and date objects from scratch

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.