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

原文地址:https://www.cnblogs.com/dreamsqin/p/13724555.html