Date type Math type Javascript type Date type Math type Javascript type Date type Math type

  • 2021-01-14 05:36:14
  • OfStack

Date type

The Date types in ECMASCript build on the earlier java.util.Date classes in Java. For this reason, the Date type stores the date using the number of milliseconds that have elapsed since midnight (0:00) of UTC(Internationally Coordinated Time) on January 1, 1970.

Create a date object

1. Create current date. No arguments need to be passed in
2. Create the specified date. To pass in a parameter, you must pass in the number of milliseconds representing the date (that is, the number of milliseconds that have elapsed between midnight on January 1, 1970 and that date). To simplify this calculation,ECMAScript provides two methods :Date.parse() and Date.UTC().


var now = new Date()// The newly created object automatically gets the current date and time 
var someDate = new Date('May 25, 2004')
var allFives = new Date(2015, 4, 4, 17, 55, 55)

Date. parse () and Date UTC ()

Date.parse()

Receive a string argument representing a date, and then try to return the number of milliseconds of the corresponding date based on that string
var someDate = new Date(Date.parse('May 25,2015'))
Note:ECMA-262 does not define which date format Date.parse() should support, so this method varies from implementation to implementation, and usually from locale to locale. In fact, passing the string representing the date to the Date constructor also calls Date.parse () in the background.

Date.UTC()

Parameters are: year, based on 0 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 2016 years 1 month 1 At midnight, 0 when 
var M = new Date(Date.UTC(2016, 0));

// GMT time 2015 years 5 month 5 On the afternoon 5:55:55
var allFives = new Date(Date.UTC(2015, 4, 4, 17, 55, 55));

Note: The Date constructor also mimics Date.UTC (), but with one notable difference: the date and time are created based on the local time zone instead of GMT. However, the Date constructor still takes the same arguments as Date.UTCf ().

Date.now()

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


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

//  Call a 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 ES79en objects to strings for the same purpose

Date formatting method

Method to format a date as a string

toDateString() toTimeString() toLocalDateString() toLocalTimeString() toUTCString()

Recommendation: toUTCString ()

Note: The UTC date refers to the date value without time zone deviation (converting 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( ):4 Give up 5 Into the 
random( )
Math.random( ) Method returns between 0 and 1 Between the 1 A random number, not including 0 and 1
var num = Math.floor(Math.random()*10, + 1)// return 1-10 Between the number of 


Related articles: