Math 与 Date
- Math 给我们提供了操作数据的一些方法, 是 JS 内置的一个对象
- Date 给我们提供了操作时间的一些方法, 是 JS 内置的一个对象
数学方法(Math)
- random / round / ceil / floor / abs
- random
- 语法:
Math.random() - 作用: 得到一个随机数, 每次生成的数字都不一样, 但一定是0~1之间的,包含0 不包含1
- 语法:
- round
- 语法:
Math.round(数字) - 作用: 将一个小数 四舍五入 变成整数
- 语法:
- ceil
- 语法:
Math.ceil(数字) - 作用: 将一个小数 向上取整
- 语法:
- floor
- 语法:
Math.floor(数字) - 作用: 将一个小数, 向下取整
- 语法:
- abs
- 语法:
Math.abs(数字) - 作用: 返回一个数字的绝对值
- 语法:
- random
- sqrt / pow / max / min / PI
- sqrt
- 语法:
Math.sqrt(数字) - 作用: 求平方根
- 语法:
- pow
- 语法:
Math.pow(基数, 幂) - 作用: 返回基数的几次幂
- 语法:
- max
- 语法:
Math.max(数字1, 数字2, 数字3, 数字4) - 作用: 返回最大值
- 语法:
- min
- 语法:
Math.min(数字1, 数字2, 数字3, 数字4) - 作用: 返回最小值
- 语法:
- PI
- 语法:
Math.PI() - 作用: 得到 π
- 语法:
- sqrt
- 封装函数: 范围内的随机整数
时间对象(Date)
时间对象的创建
- 语法:
var timer = new Date() - 不传参
- 默认返回当前时间
var timer = new Date() console.log(timer) // Fri Oct 07 2022 08:52:29 GMT+0800 (中国标准时间) - 传参
- 可以获取到一个你传递进去的时间
var timer = new Date('2022-01-06 12:12:12') console.log(timer) // Thu Jan 06 2022 12:12:12 GMT+0800 (中国标准时间)- 传递两个数字, 第一个为年, 第二个为月
var timer = new Date(2022, 00) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月 console.log(timer) // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)- 传递三个数字, 前两个不变, 第三个表示该月份的第几天, 从1到31
var timer = new Date(2022, 00, 31) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月 console.log(timer) // Mon Jan 31 2022 00:00:00 GMT+0800 (中国标准时间)- 传递四个数字, 前三个不变, 第四个表示当天的几点, 0-23
var timer = new Date(2022, 00, 31, 23) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月 console.log(timer) // Mon Jan 31 2022 23:00:00 GMT+0800 (中国标准时间)- 传递五个数字, 前四个不变, 第五个为该小时的多少分钟, 0-59
var timer = new Date(2022, 00, 31, 23, 59) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月 console.log(timer) // Mon Jan 31 2022 23:59:00 GMT+0800 (中国标准时间)- 传递六个参数, 前五个不变, 第六个表示该分钟的多少秒, 0-59
var timer = new Date(2022, 00, 31, 23, 59, 59) // 注意 月份从 0 开始计数, 0表示1月, 11表示12月 console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)- 传递字符串的形式
console.log(new Date('2019')) // Tue Jan 01 2019 08:00:00 GMT+0800 (中国标准时间) console.log(new Date('2019-02')) // Fri Feb 01 2019 08:00:00 GMT+0800 (中国标准时间) console.log(new Date('2019-02-28')) // Thu Feb 28 2019 08:00:00 GMT+0800 (中国标准时间) console.log(new Date('2019-02-28 13:')) // Thu Feb 28 2019 13:00:00 GMT+0800 (中国标准时间) console.log(new Date('2019-02-28 13:13:')) // Thu Feb 28 2019 13:13:00 GMT+0800 (中国标准时间) console.log(new Date('2019-02-28 13:13:13')) // Thu Feb 28 2019 13:13:13 GMT+0800 (中国标准时间)
将日期对象格式化成指定内容
- 我们得到的时间字符串是:
Thu Feb 28 2019 13:13:13 GMT+0800 (中国标准时间) - 我们得到这个日期中是那年或者那天, 需要靠截取字符串的形式得到
- 但是现在 JS 为我们提供了一系列的方法来得到里面的指定内容
getFullYear
得到指定字符串中的那一年
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getFullYear()) // 2022
getMonth
得到指定字符串中的那一月, 月的计数从 0 开始
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getMonth()) // 0
getDate
得到指定字符串中的那一日
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getDate()) // 31
getHours
得到指定字符串的那小时
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getHours()) // 23
getMinutes
得到指定字符串中的那分钟
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getMinutes()) // 59
getSeconds
得到指定字符串中的那秒钟
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getSeconds()) // 59
getDay
获取当前日期是一周中的 第几天(周日是0, 周六是6)
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getDay()) // 1
getTime
按照 格林威治时间计算 从1970 年 1 月 1 日 0 时 0 分 0 秒 到现在(或指定日期)的毫秒数
var timer = new Date(2022, 00, 31, 23, 59, 59)
console.log(timer) // Mon Jan 31 2022 23:59:59 GMT+0800 (中国标准时间)
console.log(timer.getTime()) // 1554681622000
设置相关
setFullYear()
- 语法:
时间对象.setFullYear(年份信息) - 作用: 修改该时间对象内的 年份信息
var time = new Date()
timer.setFullYear(2008)
console.log(timer.getFullYear())
setMonth()
- 语法:
时间对象.setMonth(月份信息) - 作用: 修改该时间对象内的 月份信息
var time = new Date()
timer.setMonth(2)
console.log(timer.getMonth())
setDate()
- 语法:
时间对象.setDate(日期信息) - 作用: 修改该时间对象内的 日期信息
var time = new Date()
timer.setDate(4)
console.log(timer.getDate())
setHours()
- 语法:
时间对象.setHours(小时信息) - 作用: 修改该时间对象内的 小时信息
var time = new Date()
timer.setHours(12)
console.log(timer.getHours())
setMinutes()
- 语法:
时间对象.setMinutes(分钟信息) - 作用: 修改该时间对象内的 分钟信息
var time = new Date()
timer.setMinutes(45)
console.log(timer.getMinutes())
setSeconds()
- 语法:
时间对象.setSecond(秒钟信息) - 作用: 修改该时间对象内的 秒钟信息
var time = new Date()
timer.setSeconds(20)
console.log(timer.getSeconds())
setMilliseconds()
- 语法:
时间对象.setMilliseconds(毫秒信息) - 作用: 修改该时间对象内的 毫秒信息
var time = new Date()
timer.setMilliseconds(857)
console.log(timer.getMilliseconds())
setTime()
- 语法:
时间对象.setTime(毫秒信息) - 作用: 使用时间戳信息直接定位时间对象
var time = new Date()
timer.setTime(1440004004531)
console.log(timer)