Javascript基礎鞏固系列——標準庫Math對象+Date對象

全手打原創,轉載請標明出處:https://www.cnblogs.com/dreamsqin/p/13724555.html, 多謝,=。=~(如果對你有幫助的話請幫我點個贊啦)

重新學習JavaScript是因爲當年轉前端有點兒趕鴨子上架的意味,我一直在反思我的知識點總是很零散,不能在腦海中形成一個完整的體系,所以這次想通過再次學習將知識點都串聯起來,結合日常開發的項目,達到溫故而知新的效果。與此同時,總結一下我認爲很重要但又被我遺漏的知識點~

Math對象

靜態屬性

  • Math.E:常數e。
  • Math.LN2:2 的自然對數。
  • Math.LN10:10 的自然對數。
  • Math.LOG2:以 2 爲底的e的對數。
  • Math.LOG10E:以 10 爲底的e的對數。
  • Math.PI:常數π。
  • Math.SQRT1_2:0.5 的平方根。
  • Math.SQRT2:2 的平方根。
Math.E // 2.718281828459045
Math.LN2 // 0.6931471805599453
Math.LN10 // 2.302585092994046
Math.LOG2E // 1.4426950408889634
Math.LOG10E // 0.4342944819032518
Math.PI // 3.141592653589793
Math.SQRT1_2 // 0.7071067811865476
Math.SQRT2 // 1.4142135623730951

靜態方法

  • Math.abs():絕對值
Math.abs(-1) // 1
  • Math.ceil():向上取整
Math.ceil(3.2) // 4
Math.ceil(-3.2) // -3
  • Math.floor():向下取整
Math.floor(3.2) // 3
Math.floor(-3.2) // -4
  • Math.max():最大值
Math.max(2, -1, 5) // 5
Math.max() // -Infinity
  • Math.min():最小值
Math.min(2, -1, 5) // -1
Math.min() // Infinity
  • Math.pow():冪運算
// 等同於 2 ** 3
Math.pow(2, 3) // 8
  • Math.sqrt():平方根
Math.sqrt(4) // 2
Math.sqrt(-4) // NaN
  • Math.log():以e爲底的自然對數
Math.log(Math.E) // 1
Math.log(10) // 2.302585092994046
  • Math.exp():e的指數
Math.exp(1) // 2.718281828459045
Math.exp(3) // 20.085536923187668
  • Math.round():四捨五入
Math.round(0.1) // 0
Math.round(0.5) // 1
Math.round(0.6) // 1
Math.round(-1.5) // -1

// 等同於
Math.floor(x + 0.5)
  • Math.random():隨機數,返回0到1之間的一個僞隨機數,可能等於0,但是一定小於1
Math.random() // 0.7151307314634323

// 任意範圍的隨機數生成函數
function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
getRandomArbitrary(1.5, 6.5)
// 2.4942810038223864

// 任意範圍的隨機整數生成函數
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
getRandomInt(1, 6) // 5

// 隨機字符串生成函數
function random_str(length) {
  var ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  ALPHABET += 'abcdefghijklmnopqrstuvwxyz';
  ALPHABET += '0123456789-_';
  var str = '';
  for (var i = 0; i < length; ++i) {
    var rand = Math.floor(Math.random() * ALPHABET.length);
    str += ALPHABET.substring(rand, rand + 1);
  }
  return str;
}

random_str(6) // "NdQKOr"
  • Math.sin():返回參數的正弦(參數爲弧度值)
  • Math.cos():返回參數的餘弦(參數爲弧度值)
  • Math.tan():返回參數的正切(參數爲弧度值)
  • Math.asin():返回參數的反正弦(返回值爲弧度值)
  • Math.acos():返回參數的反餘弦(返回值爲弧度值)
  • Math.atan():返回參數的反正切(返回值爲弧度值)

Date對象

以國際標準時間(UTC)1970年1月1日00:00:00作爲時間的零點,可以表示的時間範圍是前後各1億天(單位爲毫秒)。

作爲普通函數調用

返回一個代表當前時間的字符串,不管是否有參數。

Date() // "Thu Sep 24 2020 15:31:01 GMT+0800 (中國標準時間)"
Date(2000, 1, 1) // "Thu Sep 24 2020 15:31:01 GMT+0800 (中國標準時間))"

作爲構造函數調用

  • 使用new命令,會返回一個Date對象的實例,其中如果不加參數,實例代表的就是當前時間。
    PS:月份參數0表示一月,依次類推;11表示12月,日期默認爲1,其餘默認爲0;如果參數超出範圍會自動折算。
var today = new Date();

today
// "Thu Sep 24 2020 15:32:08 GMT+0800 (中國標準時間)"

// 等同於
today.toString()
// "Thu Sep 24 2020 15:32:08 GMT+0800 (中國標準時間)"

// 參數爲時間零點開始計算的毫秒數
new Date(1378218728000)
// Tue Sep 03 2013 22:32:08 GMT+0800 (CST)
new Date(-1378218728000)
// Fri Apr 30 1926 17:27:52 GMT+0800 (CST)

// 參數爲日期字符串
new Date('January 6, 2013');
// Sun Jan 06 2013 00:00:00 GMT+0800 (CST)

// 參數爲多個整數,
// 代表年、月、日、小時、分鐘、秒、毫秒
new Date(2013, 0, 1, 0, 0, 0, 0)
// Tue Jan 01 2013 00:00:00 GMT+0800 (CST)
  • 只要是能被Date.parse()方法解析的字符串,都可以當作參數。
new Date('2013-2-15')
new Date('2013/2/15')
new Date('02/15/2013')
new Date('2013-FEB-15')
new Date('FEB, 15, 2013')
new Date('FEB 15, 2013')
new Date('February, 15, 2013')
new Date('February 15, 2013')
new Date('15 Feb 2013')
new Date('15, February, 2013')
// Fri Feb 15 2013 00:00:00 GMT+0800 (CST)

日期實例運算

做加法時爲字符串拼接,做減法時返回間隔的毫秒數。

var d1 = new Date(2000, 2, 1);
var d2 = new Date(2000, 3, 1);

d2 - d1
// 2678400000
d2 + d1
// "Sat Apr 01 2000 00:00:00 GMT+0800 (CST)Wed Mar 01 2000 00:00:00 GMT+0800 (CST)"

靜態方法

  • Date.now():返回當前時間距離時間零點(1970年1月1日 00:00:00 UTC)的毫秒數,相當於 Unix 時間戳乘以1000。
Date.now() // 1600932635637
  • Date.parse():用來解析日期字符串,返回該時間距離時間零點(1970年1月1日 00:00:00)的毫秒數,如果解析失敗返回NaN
Date.parse('Aug 9, 1995')
Date.parse('January 26, 2011 13:51:50')
Date.parse('Mon, 25 Dec 1995 13:30:00 GMT')
Date.parse('Mon, 25 Dec 1995 13:30:00 +0430')
Date.parse('2011-10-10')
Date.parse('2011-10-10T14:48:00')
  • Date.UTC():將年、月、日等變量作爲參數,返回該時間距離時間零點(1970年1月1日 00:00:00 UTC)的毫秒數。
// 用法
Date.UTC(2011, 0, 1, 2, 3, 4, 567)
// 1293847384567

實例方法(僅列舉我覺得可能會用到的)

  • Date.prototype.toUTCString():返回對應的 UTC 時間,也就是比北京時間晚8個小時。
var d = new Date(2013, 0, 1);

d.toUTCString()
// "Mon, 31 Dec 2012 16:00:00 GMT"
  • Date.prototype.toDateString():返回日期字符串(不含小時、分和秒)。
var d = new Date(2013, 0, 1);
d.toDateString() // "Tue Jan 01 2013"
  • Date.prototype.toTimeString():返回時間字符串(不含年月日)。
var d = new Date(2013, 0, 1);
d.toTimeString() // "00:00:00 GMT+0800 (中國標準時間)"
  • getTime():返回實例距離1970年1月1日00:00:00的毫秒數,等同於valueOf方法。
  • getDate():返回實例對象對應每個月的幾號(從1開始)。
  • getDay():返回星期幾,0(星期天)到 6(星期六)。
  • getFullYear():返回四位的年份。
  • getMonth():返回月份(0表示1月,11表示12月)。
  • getHours():返回小時(0-23)。
  • getMilliseconds():返回毫秒(0-999)。
  • getMinutes():返回分鐘(0-59)。
  • getSeconds():返回秒(0-59)。
  • getTimezoneOffset():返回當前時間與 UTC 的時區差異,以分鐘表示,返回結果考慮到了夏令時因素。
var d = new Date('January 6, 2013');

d.getDate() // 6
d.getMonth() // 0
d.getFullYear() // 2013
d.getTimezoneOffset() // -480 表示 UTC 比當前時間少480分鐘,即當前時區比 UTC 早8個小時。
  • setDate(date):設置實例對象對應的每個月的幾號(1-31),返回改變後毫秒時間戳。
  • setFullYear(year [, month, date]):設置四位年份。
  • setHours(hour [, min, sec, ms]):設置小時(0-23)。
  • setMilliseconds():設置毫秒(0-999)。
  • setMinutes(min [, sec, ms]):設置分鐘(0-59)。
  • setMonth(month [, date]):設置月份(0-11)。
  • setSeconds(sec [, ms]):設置秒(0-59)。
  • setTime(milliseconds):設置毫秒時間戳。
// 相對時間計算
var d = new Date();

// 將日期向後推1000天
d.setDate(d.getDate() + 1000);
// 將時間設爲6小時後
d.setHours(d.getHours() + 6);
// 將年份設爲去年
d.setFullYear(d.getFullYear() - 1);

參考資料

JavaScript 語言入門教程 :https://wangdoc.com/javascript/index.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章