JavaScript Reference type Time date
The date type in JavaScript is built on the Java.util.Date class in early java. To do this, the date type uses the number of milliseconds to elapse since UTC January 1, 1970 Zero to save the dates. Under the condition that this data storage format is used, the date type can be saved in the exact 285 616 years before or after January 1, 1970.
Create a Date Object
In JavaScript, you can use the date () constructor to create a date object, such as:
Copy Code code as follows:
When a date parameter is not passed to the constructor, an object with the current date and time is created.
Of course, if you want to create a Date object based on a specific date and time, it's OK to just pass in the constructor with the parameter that represents the date.
The commonly used date formats that the date () constructor can accept are:
"Month/day/year", as in 2/27/2014;
"English month name day, year", such as February 27,2014;
"Year, month, day, time, minutes, seconds, milliseconds", such as 2014,1,27,11,22,22
The following format is used to create a Date object, respectively:
var date1=new Date ("2/27/2014");
alert (date1); Thu Feb 2014 00:00:00 gmt+0800
var date2=new Date ("February 27,2014");
alert (DATE2); Thu Feb 2014 00:00:00 gmt+0800
var date3=new Date (2014,1,27,11,24,0);
alert (date3); Thu Feb 2014 11:24:00 gmt+0800
var date4=new Date (2014,1,27);
alert (DATE4); Thu Feb 2014 00:00:00 gmt+0800
var date5=new Date ("2014,1,27,11,24,0");
alert (DATE5); Invalid Date
From the above example, you may notice the difference between them:
First, when you create a Date object in the previous two ways, you must pass it as a string as a parameter, and the third way to create it cannot be passed in as a string, and each value will have to be passed in as a separate value.
Second, one thing you must pay special attention to is that when you create a date in a third way, its month starts with 0, that is, January corresponds to 0, and so on, and the first two methods are normal month representations, that is, February corresponds to 2.
Third, when the third way is expressed, the years are required, and when the other parameters are omitted, they are expressed in 0.
Note: The previous two methods will be consistent with the call Date.parse () method shown, and the third way is consistent with the result of the call DATE.UTC () method displayed.
Methods of inheritance
The date type also inherits the ToString (), tolocalestring (), and valueof () methods. The format of the values that are obtained by invoking these methods will vary depending on the browser. Specifically, you can try the call under.
Date Formatting method
The date type also has some specific methods for formatting dates into strings, as follows:
toDateString ()--in a specific and implementation format to display the days of the week, month, day, year;
toTimeString ()--Displays the time, minutes, seconds, time zone in the implementation-specific format;
toLocaleDateString ()--Displays the week, month, day, and year in a locale-specific format;
toLocaleTimeString ()--Displays the time, minutes, and seconds in a realistic format;
toUTCString ()--Displays the full UTC date in a display-specific format
The above methods may be relatively infrequently used, so they are not discussed in depth.
Date/Time Component method
Array of JavaScript reference types
[Overview of 22 methods of arrays]
Arrays in JavaScript are quite different from arrays in most other languages. Although both JavaScript arrays and arrays in other languages are ordered lists of data, each item of a JavaScript array can hold data of any type, unlike other languages. That is, you can save the string with the first position of the array, save the value in the second position, and save the object in a third position. Also, the size of the JavaScript array can be dynamically adjusted, that is, you can automatically grow the data as it is added to accommodate the new data.
Array: Each item in an array can hold data of any type, and the size of the array is dynamically adjusted (up to 4294967295 items, about 4.3 billion)
[1.1] Array creation:
[1.1.1] uses the array constructor (you can also omit the new operator when you use the array constructor)
e.g. var colors = new Array ();
var colors = new Array;
var colors = new Array (' Red ', ' blue ', ' green ');
var colors = Array (3);
[note] If you pass a numeric value, the array that contains the given number of items is created according to that number;
If you pass a parameter of another type, you create an array of only one that contains that value
e.g. var colors = new Array (3);//three-item array
var colors = new Array (' Greg ');//contains an array of "Greg"
[1.1.2] uses an array literal notation (the array constructor is not invoked with this method)
e.g. var colors = [' Red ', ' blue ', ' green '];
var colors = [];
[Not available] var colors = [1, 2,];
There are three items in IE8 and before, and each item is an array of 1, 2, and undefined. In other browsers, arrays that contain only 1 and 2
[Not available] var colors = [,,,];
An array of 4 items is created in IE8 and previously, and 3 items are created in other browsers
[1.2] Array read-write
[1.2.1] When reading and setting the value of an array, you use the square brackets and provide a 0-based numeric index of the corresponding value, and the number of items in the array is kept in its length property, which always returns 0 or greater values
The length property of the [1.2.2] array is readable and writable, and you can remove items from the end of the array or add new items to the array by setting the length property of the array
e.g. var colors = [' Red ', ' blue ', ' green '];
Colors.length = 2;
Alert (colors[2]);//undefined
colors.length = 4;
Alert (colors[3]);//undefined
[1.2.3] Use the Length property to easily add new items to the end of the array
e.g. colors[colors.length] = ' black ';
[1.2.4] When you place a value above the size of the array, the array recalculates its length value, which is equal to the index of the last item plus 1.
e.g. var colors = [' Red ', ' blue ', ' green '];
COLORS[99] = ' black ';
[1.3] Array detection
[1.3.1]if (value instanceof Array) {}: The problem is that it assumes that there is only one global execution environment, and if a Web page contains more than one frame, there are actually more than two different global environments, so there are more than two different versions of the array constructor. If you pass an array to another frame from one frame, the passed-in array has its own different constructors than the array created in the second frame.
[1.3.2] ECMAScript5 added the Array.isarray () method: if (Array.isarray (value)) {}. The purpose of this method is to ultimately determine whether a value is an array, regardless of the global environment in which it was created
[1.4] Array conversions
[note] If the value of an item in an array is null or undefined, the value is represented in an empty string in the result returned by the join (), tolocalestring (), toString (), and valueof () methods
[1.4.1]tostring (): Returns a comma-delimited string of strings that are concatenation of each value in the array
[1.4.2]valueof (): Returns an array of
e.g. var colors = [' Red ', ' blue ', ' green '];
Console.log (Colors.valueof ());//[' Red ', ' blue ', ' green ']
alert (colors.valueof ());//' Red,blue,green '
Alert (colors.tostring ());//' Red,blue,green '
alert (colors);//' Red,blue,green '
[note] because alert () receives a string parameter, it calls the ToString () method in the background and gets the same result as the ToString () method
[1.4.3]tolocalestring (): It creates a comma-delimited string of array values, and the value of each item calls the toLocaleString () method
var Person1 = {
tolocalestring:function () {return
' Nikolaos ';
},
tostring:function () {return
' Nicholas ';
}
;
var Person2 = {
tolocalestring:function () {return
' Grigorios ';
},
tostring:function () {
Return ' Greg ';
}
;
var people = [Person1,person2];
alert (people);//nicholas,greg
alert (people.tostring ());//nicholas,greg
alert (people.tolocalestring () );//nikolaos,grigorios
[1.4.4]join: You can use different delimiters to build this string, the join receives only one character, serves as the delimiter string, and then returns a string containing all the array items
e.g var colors = [' red ', ' green ', ' Blue '];
Alert (Colors.join (', '));//' Red,green,blue '
alert (colors.join (' | |) '); /' red| | green| | Blue '
alert (Colors.join ());//' Red,green,blue '
alert (colors.join (undefined));//' Red,green,blue '
[note] Use undefined as a separator in IE7 and before
[1.5] Array method
[1.5.1] Stack method: The stack is a LIFO (last-in-first-out) LIFO data structure, that is, the most recently added item was first removed. The insertion of items in the stack (called push) and removal (called pop-up) only occurs at the top of the stack.
[1.5.1.1]push () Method: You can receive any number of arguments, add them individually to the end of the array, and return the length of the modified array.
[1.5.1.2]pop () method: Removes the last item from the end of the array, reduces the length value of the array, and then returns the removed item.
[1.5.2] Queue method: A queue is a FIFO (first-in-first-out) first-in, first-out data structure in which the queue adds items at the end of the list and removes items from the front of the list.
[1.5.2.1]shift (): Moves the first item in the divisor group and returns the item, while the length of the array is reduced by 1 (combined with shift () and push () to simulate the queue)
[1.5.2.2]unshift (): Add any items to the front of the array and return the new array length (combined with unshift () and pop () to simulate the queue in the opposite direction)
Attention IE7 and the following unshift () methods always return undefined
[1.5.3] Sort method:
[1.5.3.1]reverse (): Reverses the order of the arrays, returning the sorted array
[1.5.3.2]sort (): Arranges array items in ascending order, the Sort method calls the ToString () method of each array item, and then compares the resulting string sort, returning the sorted array
[Note the]sort () method can accept a comparison function as an argument to specify which value precedes which value. The comparison function receives two arguments, returns a negative number if the first argument should precede the second argument, and returns 0 if the two arguments are equal, and returns a positive number if the first argument should be at the second argument
[Comparison function] (using: e.g. Array1.sort (compare);)
function Compare (value1,value2) {
if (value1 < value2) {
return-1;
} else if (value1 > value2) {return
1;
} else{return
0;
}
For numeric types or valueof () methods, the object type of the numeric type is returned, and the comparison function can be simplified to:
function Compare (value1,value2) {return
value2-value1;
}
[Tips]: Use the following methods to create a random array
function Compare () {return
math.random ()-0.5;
}
[1.5.4] Action method (cut, connect, insert, delete, replace):
[1.5.4.1]concat (): Creates a new array based on all items in the current array, creates a copy of the current array, then adds the received parameter to the end of the replica, and finally returns the newly constructed array (concat () without affecting the original array)
[Note 1] When no arguments are passed to the concat () method, it simply copies the current array
[Note 2] if the argument is one or more arrays, the method adds each item in those arrays to the result array
[Note 3] If the value passed is not an array, these values are simply added to the end of the result array
e.g. var numbers = [1,2];
Console.log (Numbers.concat ()),//[1,2]
console.log (Numbers.concat ([5,4,3],[3,4,5],1,2));//[ 1,2,5,4,3,3,4,5,1,2];
[1.5.4.2]slice (): Creates a new array based on one or more items in the current array, accepting one or two arguments, that is, to return the starting and ending positions of the items, and finally to return the new Array (slice () without affecting the original array)
[Note 1] returns the original array when there are no arguments
[Note 2] when there is only one argument, the slice () method returns all items starting at the specified position from the parameter to the end of the current array
[Note 3] Two arguments, this method returns the entry between the start and end positions, but does not include the end position of the item
[Note 4] If the argument is negative, use the array length plus negative numbers as arguments
[Note 5] If the end position is less than the start position, an empty array is returned
var numbers = [1,2,3,4,5];
Console.log (Numbers.slice ());//[1,2,3,4,5]
Console.log (Numbers.slice (2));//[3,4,5]
Console.log ( Numbers.slice (2,3)),//[3]
console.log (Numbers.slice ( -3)),//-3+5=2-> [3,4,5]
Console.log ( Numbers.slice (2,1));//[]
[1.5.4.3]splice (): The original array changes to the modified array, and splice () returns an array of items that are deleted from the original array, and an empty array if no deletion is made
[A] Delete: Two parameters are the position of the first item to be deleted, the number of items to delete
[b] Insert: Three parameters are starting position, 0 (cardinality to delete), items to insert
[C] Replace: Three parameters are the starting position, number of items to delete, items to insert
[Note 1] If the first argument is negative, use the array length plus negative numbers as arguments
[Note 2] If the second argument is a negative number, use 0 as the argument
var numbers = [1,2,3,4,5];
Console.log (Numbers.splice (0,2), numbers)//[1,2] [3,4,5]
var numbers = [1,2,3,4,5];
Console.log (Numbers.splice (1,0,11,12), numbers)//[] [1,11,12,2,3,4,5]
var numbers = [1,2,3,4,5];
Console.log (Numbers.splice (1,3,11,12), numbers)//[2,3,4] [1,11,12,5]
var numbers = [1,2,3,4,5];
Console.log (Numbers.splice ( -4,3,11,12), numbers);//-4+5=1-> [2,3,4] [1,11,12,5]
var numbers = [1,2,3,4,5];
Console.log (Numbers.splice ( -4,-3,11,12), numbers)//-4+5=1-> [] [1,11,12,2,3,4,5]
[1.5.5] Location method (ECMASCRIPT5): Two parameters: An item to find, an index that represents the position at which to find the start (optional). Returns the position of the first lookup item that satisfies the condition in the array, or 1 if not found (the location method does not affect the original array)
[note] When comparing, use the congruent operator
[1.5.5.1]indexof ()
[1.5.5.2]lastindexof ()
var person = {name: ' Nicholas '};
var people = [{name: ' Nicholas '}];
var morepeople = [person];
alert (people.indexof);//-1, because person and people[0], although the values are the same, are two reference
alert (morepeople.indexof); 0 because person and morepeople[0] are the same reference
alert (Morepeople.indexof ({name: ' Nicholas '}));//-1 because it is not the same reference
[Tips] If you return all index values for an item that satisfies a condition
function Allindexof (array,value) {
var result = [];
var pos = array.indexof (value);
if (pos = = 1) {
return-1
}
while (Pos >-1) {
Result.push (POS);
pos = Array.indexof (value,pos+1);
}
return result;
}
var array = [1,2,3,3,2,1];
Console.log (Allindexof (array,1));//[0,5]
[1.5.6] Iteration method (ECMASCRIPT5): Two parameters: Optionally, the function to run on each item, the value that runs the function scope object, that affects this. Functions passed into these methods receive three parameters: the value of the array item, the position of the item in the array, the array object itself (the iteration method does not affect the original array)
[1.5.6.1]every (): Runs the given function for each item in the array, returns TRUE if the function returns true for each item
[1.5.6.2]filter (): Each item in an array runs the given function, returning an array of items that the function returns True (often used to query all array items that match the criteria)
[1.5.6.3]foreach (): Each item in the array runs the given function, and this method has no return value (equivalent to a for loop)
[1.5.6.4]map (): Each item in an array runs the given function, returning an array of the results of each function call (commonly used to create an array that corresponds to another array of one by one)
[1.5.6.5]some (): Runs the given function for each item in the array, returns TRUE if the function returns true on either item
var numbers = [1,2,3,4,5,6,7,8,9,0];
var sum = 0;
var everyresult = numbers.every (function (Item,index,array) {return (ITEM>2);
});
var filterresult = numbers.filter (function (Item,index,array) {return (ITEM>2)});
var foreachresult = Numbers.foreach (function (item,index,array) {sum + = Item;
Return (ITEM>2)});
var mapresult = Numbers.map (function (Item,index,array) {return (item*2)});
var som = numbers.some (function (Item,index,array) {return (ITEM>2)}); Console.log (Everyresult);//false Console.log (Filterresult);//[3,4,5,6,7,8,9] Console.log (foreachresult,sum); Undefined Console.log (mapresult);//[2,4,6,8,10,12,14,16,18,0] Console.log (someresult);//true [Tips] function
Logarray (Value,index,array) {console.log (value); [2,5,,,,, 9].foreach (Logarray)//2 5 9
[1.5.7] Merge method (ECMASCRIPT5): All the items of an iterative group, building a final returned value. Receives two parameters: an optional function called on each item, as the initial value for the merge base. Functions passed to reduce () and reduceright () Accept 4 Parameters: The previous value, the current value, the index of the item, and the array object. Any value returned by this function will be automatically passed to the next item as the first argument. The first iteration occurs on the second item of the array. Therefore, the first argument is the first item in the array, and the second is the second item of the array (the merge method does not affect the original array)
[1.5.7.1]reduce ()
[1.5.7.2]reduceright ()
var sum = values.reduce (function (prev,cur,index,array) {return
prev+cur;
})
alert (sum);//15