JS date, Math, array, and collation of objects

Source: Internet
Author: User
Tags add time getdate string format

Related Topics Knowledge points

Related Topics get date of 2017-06-10 format

function formatdate (DT) {
    if (!DT) {
        dt = new Date ();
    }
    var year = Dt.getfullyear ();
var month = Dt.getmonth () +1;
var date = Dt.getdate ();
if (month<10) {
    //force type conversion
    month = ' 0 ' +month;
}
if (date<10) {
    date = ' 0 ' +date;
}
Return year +  '-' +month+ '-' +date;
}
var dt = new Date ();
var formatdate = formatdate (dt);
Console.log (format);
Gets the random number, which requires a consistent length of string format (the role of random numbers in front-end development)
var random =math.random ();
random= random + ' 0000000000 '//guarantee the next sentence will not be an error
random = Random.slice (0,9);
Console.log (random);
Write a generic foreach function that traverses objects and arrays (in jquery)
Think of the foreach in the object and the in array for in how to use better

//Here is the foreach function Functions
foreach (OBJ,FN) {
    var key;
    if (obj instanceof Array) {
        Obj.foreach (function (item,index) {
            fn (index.item);
        })
    } else{for
        (key in obj) {
            fn (Key.obj[key]);
}}} Call
var arr = [1,2,3];
ForEach (Arr,function (Index,item)) {
    console.log (index.item);
}

var obj = {x:100,y:200};
ForEach (Obj,function (key,value) {
    console.log (key,value);
})
Knowledge pointsDate
Date.nowtime (); Gets the current number of milliseconds (this is the number of milliseconds since 1970 to go now)
var mydate = new Date ();//new a Time Object
mydate.gettime ()///Get the number of
milliseconds Mydate.getfullyear (); Year (4 digits), there is also a getyear () method, there is a compatibility problem, with getFullYear () difference 1900
mydate.getmonth ();//month (0-11), the actual application is should pay attention to its return value
Mydate.getdate ()//day (1-31)
mydate.getday ()//week (0-6), the actual application is to pay attention to its return value
mydate.gethours ();//Time (0-23) Mydate.getminutes ()///
min (0-59)
mydate.getseconds ();//sec (0-59)
Math
1. Get random number math.random ();

Returns a decimal number greater than 0 or less than 1. eg:0.14249200181060218, commonly used to clear the cache, for example: When the page is open, the data that has just been updated can sometimes not be refreshed on the page because of caching, because if the requested address is the same, the browser will not connect to the server. This can be handled by random numbers, which is equivalent to loading a new page.

After the URL parameter, add "? r=" + math.random (),//plus random number
after the URL parameter plus "? timestamp=" + New Date (). GetTime ()//can also add time stamp

2. More methods and random number expansion see my previous blog; array API
1. foreach traverses all elements

var arr = [1,2,3];
Arr.foreach (function (item,index) {//item is the position of the element whose value Eg:1,2,3,index bit is the element, starting from 0
    //traversing all elements in the array
    console.log (index, Item);
    Note Parameter Order
});

2. Every determine if all elements are eligible

var arr = [1,2,3];
var result = Arr.every (Funciton (item,index) {
//is used to determine all array elements that satisfy a condition
    if (item < 4) {return
        ture;
    }
});
Console.log (result);//true

3. Some determine if at least one element meets the criteria

var arr = [1,2,3];
var result = Arr.every (Funciton (item,index) {
//is used to determine all array elements that satisfy a condition
    if (item < 2) {return
        ture;
    }
} );
Console.log (result);//true

4. Sort sorting (small to large sort)

var arr = [1,4,3,2,5];
var arr2 = Arr.sort (function (a,b) {
    //from small to large return
    a-b;
    From large to small
    //return b-a;
});
Console.log (ARR2);//[1,2,3,4,5]

5. The map assembles the elements and generates a new array (the elements are assembled into another element in a single rule, generating a new array)

var arr = [1,2,3,4];
var arr2 = Arr.map (function (item,index) {
    //reassemble elements and return
    ' <b> ' +item+ ' </b> ';
});
Console.log (ARR2);  ["<b>1</b>", "<b>2</b>", "<b>3</b>", "<b>4</b>"]

6. Filter filters for eligible elements

var arr = [1,2,3];
var arr2 = arr.filter (function (item,index) {
    //is filtered by a condition array
    if (item>=2) {return
        true;
    }
});
Console.log (ARR2); 2,3
Object API
var obj ={x:100,y:200,z:300};
var key;
for (key in obj) {//key Here is the attribute name of obj (
    obj.hasownproperty (key)) {//To determine that he is the attribute of this obj rather than the stereotype of the attribute
        Console.log (key, Obj[key]);
    }

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.