The date type in JavaScript and the math type in detail _ basics

Source: Internet
Author: User
Tags local time

Date type

The date type in ECMAScript is built on the Java.util.Date class in early java. This date type uses the number of milliseconds since UTC (International coordinated Time) to start at midnight (0 o'clock) of January 1, 1970 to save dates.

Create a Date Object

1. Create the current date. No incoming arguments required
2. Create the specified date. You need to pass in the parameter, and you must pass in the number of milliseconds that represent the date (that is, the number of milliseconds from midnight on January 1, 1970 to that date). To simplify this computational 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, ")
var allfives = new Date (2015, 4, 4 , 17, 55, 55)

Date.parse () and DATE.UTC ()

Date.parse ()

Receives a string parameter representing the date, and then attempts to return the number of milliseconds for the corresponding date based on this string
var somedate = new Date (Date.parse (' May 25,2015 '))
note:ecma-262 does not define DATE.PARSE () should support that date format, so this method varies by implementation and usually varies by region. The string that represents the date is actually passed to the date constructor, and Date.parse () is also invoked in the background.

DATE.UTC ()

The parameters are: year, month, day, hour, minute, second, and number of milliseconds based on 0. Only the first two parameters are required. If other arguments are omitted, the assumption is 0.

GMT time January 1, 2016 midnight 0 o'clock
var M = new Date (DATE.UTC (2016, 0));

GMT time May 5, 2015 PM 5:55:55
var allfives = new Date (DATE.UTC (2015, 4, 4, 17, 55, 55));

The Note:date constructor also imitates DATE.UTC (), but there is one obvious difference: the date and time are created based on the local time zone rather than GMT. However, the date constructor is still the same as the parameter received by DATE.UTCF ().

Date.now ()

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

Get start time
var start = Date.now ();

Call function
dosomthing ();

Get 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 a Date object to a string, or to achieve the same purpose

Date Formatting method

Ways to format dates as strings

    1. toDateString ()
    2. toTimeString ()
    3. Tolocaldatestring ()
    4. Tolocaltimestring ()
    5. toUTCString ()

Recommendation: toUTCString ()

The NOTE:UTC date refers to a date value that has no time zone deviation (converts the date to GMT time).

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 (): Rounding up
Math.floor (): Rounding down
math.round (): Rounding
random ()
math.random () method returns a random number between 0 and 1. Excluding 0 and 1
var num = Math.floor (Math.random () *10, + 1)//Returns the number between 1-10

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.