JS Basics 8-Common built-in objects (date and math)

Source: Internet
Author: User
Tags pow square root

First, Date Object

Creation of 1.Date objects

new Date () New Date (month dd,yyyy hh:mm:ss) new Date (YYYY,MTH,DD,HH,MM,SS) new Date (YYYY,MTH,DD) new Date (ms)
    <script type= "Text/javascript" >        var d1=New Date ();   The first method created is    document.write (d1.tostring () + "<br>");     // second method    of Creation var d2=New Date ("2009-02-28 18:18:18");        document.write (D2.todatestring ());             </script>

Acquisition of 2.Date objects

GetDate ()

returns a day in one months from a Date object (1~31)

GetDay ()

returns a day of the week from a Date object (0~6)

GetMonth ()

Returns the month from the Date object (0~11)

getFullYear ()

returns the year as a four-digit number from a Date Object

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 of the Date object (0~59)

GetTime ()

Returns the number of milliseconds in 1970 year 1 months 1 days to present

    var year=d1.getfullyear ();     var month=d1.getmonth () +1;     var date=d1.getdate ();     var day=d1.getday ();        document.write (year + "Years" +month+ "month" +date+ "Day" +day);    

Settings for 3.Date objects

SetDate ()

Set a day of the month in a Date object (1~31)

Setmonth

Set the month in the Date object (0~11)

setFullYear ()

Set the year in the Date object (four digits)

Sethours ()

Set the hour in the Date object (0~23)

Setminutes ()

Set the minutes in a Date object (0~59)

Setseconds ()

Sets the second of the Date object (0~59)

Setmilliseconds ()

Sets the milliseconds in the Date object (0~999)

<script type= "Text/javascript" >//set Date is 2018.1.18 daysD1.setfullyear (2018); D1.setmonth (0); D1.setdate (18);//get date of January 18, 2018    varYear=d1.getfullyear (); varMonth=d1.getmonth (+1); varDate=d1.getdate (); varday=D1.getday ();//Judging January 18, 2018 is the day of the week    Switch(day) { Case0: Day= "Sunday";  Break;  Case1: Day= "Monday";  Break;  Case2: Day= "Tuesday";  Break;  Case3: Day= "Wednesday";  Break;  Case4: Day= "Thursday";  Break;  Case5: Day= "Friday";  Break;  Case6: Day= "Saturday";  Break; default: Day= "Error Data"    }//Date of Output January 18, 2018document.write (year+ "year" +month+ "month" +date+ "Day" +Day ); </script>

Second, the Math object

1. Common methods

ABS (x)

The absolute value of the return number

Ceil (x)

Rounding on a logarithmic

Floor (x)

Rounding with logarithms

Max (x, y)

Returns the highest value in x and y

Min (x, y)

Returns the lowest value in x and y

Pow (x, y)

Returns the y power of X

Random ()

Returns the number of random numbers between 0~1

Round (x)

Rounding a number to the nearest integer

sqrt (x)

The square root of the returned number

Sin (x) returns the sinusoidal value of a number

<!doctype html>varA=math.abs (-1); document.write (A+ "<br>"); varB=math.ceil (11.2);//Rounding ondocument.write (b + "<br>"); varC=math.floor (11.2);//Rounding Downdocument.write (c+ "<br>"); varD=math.max (50,12);//To find the maximum valuedocument.write (d+ "<br>"); varE=math.min (50,12);//To find the minimum valuedocument.write (e+ "<br>"); varF=math.pow (2,3);//3 powers of 2document.write (f+ "<br>"); varG=math.random ();//random number between 0-1document.write (g+ "<br>"); varH=math.round (3.7);//Roundingdocument.write (H + "<br>"); varI=MATH.SQRT (6);//Find square rootdocument.write (i+ "<br>"); //π to find the area of a circle:    varCircle=math.pow (2,2) *Math.PI;        document.write (circle); </script>

JS Basics 8-Common built-in objects (date and math)

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.