Description of Date and Math types in Javascript, datemath

Source: Internet
Author: User

Description of Date and Math types in Javascript, datemath

Date type

The Date type in ECMASCript is built on the Java. util. Date class in java in the early stage. Therefore, the Date type uses the number of milliseconds that elapsed since midnight (0 hours) on January 1, January 1, 1970 in UTC (International Coordination time) to save the Date.

Creation date object

1. Create the current date. No parameter required
2. Create a specified date. You must specify the number of milliseconds that indicate the date (that is, the number of milliseconds that have elapsed since midnight, January 1, January 1, 1970 to the end of the date ). To simplify this computation process, ECMAScript provides two methods: Date. parse () and Date. UTC ().

Var now = new Date () // the newly created object automatically obtains the current Date and time var someDate = new Date ('may 25,200 4') var allFives = new Date (2015, 4, 4, 17, 55, 55)

Date. parse () and Date. UTC ()

Date. parse ()

Receives a string parameter that represents the date, and then tries to return the millisecond value of the corresponding date based on the string.
Var someDate = new Date (Date. parse ('may 25,2015 '))
Note: The ECMA-262 does not define that Date. parse () should support that Date format, so this method varies by implementation, and usually varies by region. In fact, the Date string is passed to the Date constructor, and Date. parse () is also called in the background ().

Date. UTC ()

The parameters are: year, based on the number of months, days, hours, minutes, seconds, and milliseconds. Only the first two parameters are required. If other parameters are omitted, all are assumed to be 0.

// GMT time, January 1, January 1, 2016 var M = new Date (Date. UTC (2016, 0); // GMT time May 5, 2015: 55var allFives = new Date (Date. UTC (2015, 4, 4, 17, 55, 55 ));

Note: The Date constructor also imitates Date. UTC (), but there is a significant difference: the Date and time are created based on the local time zone rather than GMT. However, the Date constructor still receives the same parameters as Date. UTCf.

Date. now ()

Returns the number of milliseconds of the date and time when this method is called.

// Obtain the start time var start = Date. now (); // call the function doSomthing (); // obtain the stop time var stop = Date. now (); result = stop-start;

Compatibility: IE9 +, Firfox3 +, Safari3 +, Opera10.5, Chrome. In browsers that do not support it, you can use the + operator to convert the Date object to a string.

Date formatting Method

Format a date as a string

  1. ToDateString ()
  2. ToTimeString ()
  3. ToLocalDateString ()
  4. ToLocalTimeString ()
  5. ToUTCString ()

Recommended: toUTCString ()

Note: UTC date refers to the date value without time zone deviation (convert the date to GMT.

Math type

Min () and max ()

var value = [1,2,3,4,5,6,7,8];var max = Math.max.apply(Math, values);

Rounding Method

Math. ceil (): rounds up Math. floor (): round down Math. round (): Rounding random () Math. the random () method returns a random number between 0 and 1, excluding 0 and 1var num = Math. floor (Math. random () * 10, + 1) // returns the number between 1 and 10.

Articles you may be interested in:
  • Differences between Javascript Math ceil (), floor (), and round () Functions
  • Summary of Math random, round, ceil, and floor usage in js
  • Use the js Math. random () function to generate random numbers between n and m.
  • JavaScript Math. ceil method (rounded up to an integer)
  • About Date (Date object) and Math object in JavaScript
  • Describes the use of the Math. abs () method in JavaScript.
  • A Brief Introduction to the javascript Date type

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.