Date (Date object) and Math object_javascript techniques in JavaScript

Source: Internet
Author: User
This article briefly introduces Date (Date object) and Math object in JavaScript. For more information, see Date object

1. What is a Date object?

A date object can store any date, and can be precise to the number of milliseconds (1/1000 seconds ).

Syntax: var Udate = new Date ();

Note: The initial value is the current time (the current computer system time ).

2. Common Methods for Date objects:

3. Date Method Instance

The Code is as follows:


Var newTime = new Date (); // get the current time
Var millSecond = Date. now (); // The number of milliseconds that the current Date is converted
Var fullYear = newTime. getFullYear (); // get the year
Var year = newTime. getYear (); // obtain the year
Var month = newTime. getMonth (); // returns 0-11 for the month. 0 indicates that the month is January 11.
Var week = newTime. getDay (); // returns a number ranging from 0 to 6 for the day of the week. 0 indicates Sunday.
Var today = newTime. getDate (); // obtain the date of the current day
Var hours = newTime. getHours (); // obtain the hours
Var minutes = newTime. getMinutes (); // get the number of minutes
Var seconds = newTime. getSeconds (); // obtain the number of seconds
Console. log (newTime); // Wed Feb 04 2015 10:54:17 GMT + 0800 (China Standard Time)
Console. log (millSecond); // 1423029309565
Console. log (fullYear); // 2015
Console. log (year); // 115
Console. log (month); // 1 indicates January 1, February
Console. log (week); // 3 indicates Wednesday
Console. log (today); // 4
Console. log (hours); // 10 hours
Console. log (minutes); // 54 minutes
Console. log (seconds); // 17 seconds

Math object

1. What is a Math object??

Math object that provides mathematical calculation of data.

Note: A Math object is an inherent object. You can call all its attributes and methods directly using Math as an object without creating it. This is the difference between Date and String objects.

2. attributes and methods of the Math object

Math Object Attributes

Math object Method

3. Example of some Math object Methods

1): The ceil () method is rounded up to return an integer greater than or equal to x and closest to x.

The Code is as follows:


Document. write (Math. ceil (0.8) +"
") // 1
Document. write (Math. ceil (6.3) +"
") // 7
Document. write (Math. ceil (5) +"
") // 5
Document. write (Math. ceil (3.5) +"
") // 4
Document. write (Math. ceil (-5.1) +"
") //-5
Document. write (Math. ceil (-5.9) //-5

2): The floor () method is rounded down to return an integer less than or equal to x and closest to x.

The Code is as follows:


Document. write (Math. floor (0.8) +"
") // 0
Document. write (Math. floor (6.3) +"
") // 6
Document. write (Math. floor (5) +"
") // 5
Document. write (Math. floor (3.5) +"
") // 3
Document. write (Math. floor (-5.1) +"
") //-6
Document. write (Math. floor (-5.9) //-6

3): The round () method rounds a number to the nearest integer.

The Code is as follows:


Document. write (Math. round (0.8) +"
") // 1
Document. write (Math. round (6.3) +"
") // 6
Document. write (Math. round (5) +"
") // 5
Document. write (Math. round (3.5) +"
") // 4
Document. write (Math. round (-5.1) +"
") //-5
Document. write (Math. round (-5.9) +"
") //-6

4): the random () method returns a value ranging from 0 ~ A random number between 1 (greater than or equal to 0 but less than 1.

The Code is as follows:


Document. write (Math. random (); // returns a number ranging from 0 to 1, excluding 1.
Document. write (Math. random () * 10); // The number between 0 and 10 is returned, excluding 10.

5): min () method: returns the minimum value of a group of values.

The Code is as follows:


Document. write (Math. min (2, 3, 4, 6); // 2

To obtain the minimum value in an array, use the apply () method:

The Code is as follows:


Var values = [,];
Document. write (Math. min. apply (Math, values) +"
"); // 1

The Math object is used as the first parameter of apply, and any array is used as the second parameter.

6): max () method: returns the maximum value of a group of values.

The Code is as follows:


Document. write (Math. max (2, 3, 4, 6); // 6

To obtain the minimum value in an array, use the apply () method:

The Code is as follows:


Var values = [,];
Document. write (Math. max. apply (Math, values) +"
"); // 9

The above is all about Date (Date object) and Math object in JavaScript. I hope you will like it.

Related Article

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.