Javascript Math對象和Date對象常用方法詳解

Math對象

Math 是js中的一個內(nèi)置對象, 它為數(shù)學常量和數(shù)學函數(shù)提供了屬性和方法,而不是一個函數(shù)對象。

屬性

  • Math.PI
    = > 圓周率,一個圓的周長和直徑之比,悅等于3.14159.
  • Math.E
    = > 歐拉常數(shù),自然對數(shù)的底數(shù),約等于2.718.

常用的方法

1. Math.abs(x)

=> 取得x的絕對值

var num = -2;
Math.abs(num); // 2

2. Math.random()

= > 返回0到1之間的一個隨機數(shù)

//隨機一個(0,1]的數(shù)字
Math.random(); // 0.29510836134846596
//隨機返回[0,num)范圍內(nèi)的數(shù)字
//Math.random()*num
Math.random*10; //8.028820852140843
//隨機返回[start,end)范圍內(nèi)的數(shù)字
//Math.random()*(end-start)+start;
Math.random()*(100-50)+50; //71.3784510483645 返回[50,100)的隨機數(shù)

3. Math.floor(x)

= > 返回小于x的最大整數(shù),通俗的講就是省略小數(shù)點后面的值,保留整數(shù),與 Math.ceil(x)是相對的。

Math.floor(-1.0);  // -1
Math.floor(2.239424);  // 2
Math.floor(3.9);  // 3

4. Math.round(x)

= > 返回四舍五入后的整數(shù)

Math.round(1.4);  // 1
Math.round(1.6);  // 2

5. Math.sin(x)

= > 返回 x 的正弦值

Math.sin(90*Math.PI/180);  // 1

6. Math.cos(x)

= > 返回 x 的余弦值

Math.cos(180*Math.PI/180);  // 1

7. Math.ceil(x)

= > 返回x向上取整后的值,通俗的講就是小數(shù)點后有值,就進一位數(shù)值

Math.ceil(-1.0);  // -1
Math.ceil(2.239424);  // 3
Math.ceil(3.9);  // 4

Date對象

Date類型是以UTC(Coordinated Universal Time,國際協(xié)調(diào)時間)1970 年1 月1 日午夜(零時)開始經(jīng)過的毫秒數(shù)來保存日期。

創(chuàng)建日期的幾種方法

new Date();  
new Date(value);  // value為時間戳
new Date(dateString);  // dateString為表示日期的字符串
new Date(year, month[,day[,hour[,minutes[,seconds[,millseconds]]]]]);
//注意:代表月份的整數(shù)值是從0(1月)到11(12月)
new Date(); // Fri Jul 22 2016 13:32:10 GMT+0800 (CST)
new Date(1469159782236); //  Fri Jul 22 2016 11:56:22 GMT+0800 (CST)
new Date("December 17, 1995 03:24:00");  //Sun Dec 17 1995 03:24:00 GMT+0800 (CST)
new Date("1995-12-17T03:24:00"); //Sun Dec 17 1995 11:24:00 GMT+0800 (CST)
new Date(1995,11,17,3,24,0);  //Sun Dec 17 1995 03:24:00 GMT+0800 (CST)

常用方法

1. Date.now()

= > 返回自 1970-1-1 00:00:00 UTC (時間標準時間)至今所經(jīng)過的毫秒數(shù)。

Date.now();  //1469166290095

2. Date.parse()

= > 解析一個表示日期的字符串,并返回從 1970-1-1 00:00:00 所經(jīng)過的毫秒數(shù)。

Date.parse('Sun Dec 17 1995 03:24:00 GMT+0800 (CST)');
//819141840000

3. getFullYear()

= > 返回指定日期對象的年份

var today = new Date();
today.getFullYear();  // 2016

4. getMonth()

= > 返回指定日期對象的月份(0-11)

var today = new Date();
today.getMonth();  // 6

5. getDate()

= > 返回指定日期對象的月份中的第幾天(1-31)

var today = new Date();
today.getDate();  // 22

6. getHours()

= > 返回指定日期對象的小時(0-23)

var today = new Date();
today.getHours();  // 14

7. getMinutes()

= > 返回指定日期對象的分鐘(0-59)

var today = new Date();
today.getMinutes();  // 40

8. getSeconds()

= > 返回指定日期對象的秒數(shù)(0-59)

var today = new Date();
today.getMinutes();  // 40

日期格式化

一個簡單的日期格式化函數(shù)

var format = function (time, format) {
    var t = new Date(time);
    var tf = function (i) {
        return (i < 10 ? '0' : '') + i
    };
    return format.replace(/YYYY|MM|DD|hh|mm|ss/g, function (a) {
        switch (a) {
        case 'YYYY':
            return tf(t.getFullYear());
            break;
        case 'MM':
            return tf(t.getMonth() + 1);
            break;
        case 'mm':
            return tf(t.getMinutes());
            break;
        case 'DD':
            return tf(t.getDate());
            break;
        case 'hh':
            return tf(t.getHours());
            break;
        case 'ss':
            return tf(t.getSeconds());
            break;
        }
    });
}

format(new Date().getTime(),'YYYY-MM-DD hh:mm:ss');
// "2016-07-22 15:08:14"
format(new Date().getTime(),'YYYY年MM月DD日 hh時mm分ss秒');
//"2016年07月22日 15時10分18秒"

最后,如果想要格式化日期獲得更好的效果,moment.js 是個不錯的js庫。

參考資料

Date

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容