Date
| Date() 返回当日的日期和时间。 | |
|---|---|
| getDate() 从 Date 对象返回一个月中的某一天 (1 ~ 31)。 | |
| getDay() 从 Date 对象返回一周中的某一天 (0 ~ 6)。 | |
| getMonth() 从 Date 对象返回月份 (0 ~ 11)。 | |
| getFullYear() 从 Date 对象以四位数字返回年份。 | |
| getYear() 请使用 getFullYear() 方法代替。 | |
| getHours() 返回 Date 对象的小时 (0 ~ 23)。 | |
| getMinutes() 返回 Date 对象的分钟 (0 ~ 59)。 | |
| getSeconds() 返回 Date 对象的秒数 (0 ~ 59)。 | |
| getMilliseconds() 返回 Date 对象的毫秒(0 ~ 999)。 | |
| getTime() 返回 1970 年 1 月 1 日至今的毫秒数。 | |
| getTimezoneOffset() 返回本地时间与格林威治标准时间 (GMT) 的分钟差。 | |
| getUTCDate() 根据世界时从 Date 对象返回月中的一天 (1 ~ 31)。 | |
| getUTCDay() 根据世界时从 Date 对象返回周中的一天 (0 ~ 6)。 | |
| getUTCMonth() 根据世界时从 Date 对象返回月份 (0 ~ 11)。 | |
| getUTCFullYear() 根据世界时从 Date 对象返回四位数的年份。 | |
| getUTCHours() 根据世界时返回 Date 对象的小时 (0 ~ 23)。 | |
| getUTCMinutes() 根据世界时返回 Date 对象的分钟 (0 ~ 59)。 | |
| getUTCSeconds() 根据世界时返回 Date 对象的秒钟 (0 ~ 59)。 | |
| getUTCMilliseconds() 根据世界时返回 Date 对象的毫秒(0 ~ 999)。 | |
| parse() 返回1970年1月1日午夜到指定日期(字符串)的毫秒数。 | |
| setDate() 设置 Date 对象中月的某一天 (1 ~ 31)。 | |
| setMonth() 设置 Date 对象中月份 (0 ~ 11)。 | |
| setFullYear() 设置 Date 对象中的年份(四位数字)。 | |
| setYear() 请使用 setFullYear() 方法代替。 | |
| setHours() 设置 Date 对象中的小时 (0 ~ 23)。 | |
| setMinutes() 设置 Date 对象中的分钟 (0 ~ 59)。 | |
| setSeconds() 设置 Date 对象中的秒钟 (0 ~ 59)。 | |
| setMilliseconds() 设置 Date 对象中的毫秒 (0 ~ 999)。 | |
| setTime() 以毫秒设置 Date 对象。 | |
| setUTCDate() 根据世界时设置 Date 对象中月份的一天 (1 ~ 31)。 | |
| setUTCMonth() 根据世界时设置 Date 对象中的月份 (0 ~ 11)。 | |
| setUTCFullYear() 根据世界时设置 Date 对象中的年份(四位数字)。 | |
| setUTCHours() 根据世界时设置 Date 对象中的小时 (0 ~ 23)。 | |
| setUTCMinutes() 根据世界时设置 Date 对象中的分钟 (0 ~ 59)。 | |
| setUTCSeconds() 根据世界时设置 Date 对象中的秒钟 (0 ~ 59)。 | |
| setUTCMilliseconds() 根据世界时设置 Date 对象中的毫秒 (0 ~ 999)。 | |
| toSource() 返回该对象的源代码。 | |
| toString() 把 Date 对象转换为字符串。 | |
| toTimeString() 把 Date 对象的时间部分转换为字符串。 | |
| toDateString() 把 Date 对象的日期部分转换为字符串。 | |
| toGMTString() 请使用 toUTCString() 方法代替。 | |
| toUTCString() 根据世界时,把 Date 对象转换为字符串。 | |
| toLocaleString() 根据本地时间格式,把 Date 对象转换为字符串。 | |
| toLocaleTimeString() 根据本地时间格式,把 Date 对象的时间部分转换为字符串。 | |
| toLocaleDateString() 根据本地时间格式,把 Date 对象的日期部分转换为字符串。 | |
| UTC() 根据世界时返回 1970 年 1 月 1 日 到指定日期的毫秒数。 | |
| valueOf() 返回 Date 对象的原始值。 |
1.代码演示
var dt = new Date() //在当前运行代码时,创建一个时间点对象
console.log(dt.getDate()) //几号
console.log(dt.getDay()) //周几 天-7
console.log(dt.getFullYear()) //4位数的年份
console.log(dt.getYear()) //3位 后两位代表年份
console.log(dt.getMonth()) //重点(0-11)
console.log(dt.getHours()) //重点-这个方法名
console.log(dt.getMinutes())//分
console.log(dt.getSeconds())//秒
console.log(dt.getMilliseconds()) //1000毫秒是1秒
console.log(dt)//打印的不是字符串,是对象
console.log("Mon May 23 2022 20:13:53 GMT+0800 (中国标准时间)")
console.log(dt.getTime())//返回 1970 年 1 月 1 日至今的毫秒数。
注意:时间传入的数据超过了一单位 会自动进位
var dt6=new Date(2002,13,34,8,30,30)
console.log(dt6)
2.具体解释
2.1 时区
整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时UTC(UTC, Universal Time Coordinated),UTC与格林尼治平均时(GMT, Greenwich Mean Time)一样,都与英国伦敦的本地时相同北京时区是东八区:+0800,领先UTC八个小时
2.2 new Date()
当前的本地日期和时间
var dt = new Date();
console.log(dt)
2.3 new Date(ms)
把毫秒数转换为Date对象,表示从'1970/01/01 00:00:00'为起点,开始叠加的毫秒数,注意:起点的时分秒还要加上当前所在的时区,北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00'
var dt = new Date(1000 * 60 * 1); // 前进1分钟的毫秒数
console.log(dt); // 1970/01/01 08:01:00
dt = new Date(-1000 * 60 * 1); // 倒退1分钟的毫秒数
console.log(dt); //1970/01/01 07:59:00
2.4 new Date(dateStr)
把字符串转换为Date对象
换为Date对象的字符串(可省略时间)的格式主要有两种:
yyyy/MM/dd HH:mm:ss(都用的这种)若省略时间,返回的Date对象的时间为 00:00:00。
yyyy-MM-dd HH:mm:ss若省略时间,返回的Date对象的时间为 08:00:00(加上本地时区),若不省略时间,此字符串在IE中会转换失败
var dt = new Date('2017/12/25'); // yyyy/MM/dd
console.log(dt); // 2017/12/25 00:00:00
dt = new Date('2017/12/25 12:00:00'); // yyyy/MM/dd HH:mm:ss
console.log(dt); //2017/12/25 12:00:00
dt = new Date('2017-12-25'); // yyyy-MM-dd
console.log(dt); //2017-12-25 08:00:00 (加上了东8区的时区)
dt = new Date('2017-12-25 12:00:00'); // yyyy-MM-dd HH:mm:ss (注意:此转换方式在IE中会报错!)
console.log(dt); // 2017-12-25 12:00:00
2.5 new Date(year, month, opt_day, opt_hours, opt_minutes, opt_seconds, opt_milliseconds)
把年月日、时分秒转换为Date对象//option
参数:
year:年份4位数字,如:1999、2014
month:月份2位数字,从0开始计算,0表示1月份、11表示12月份。
opt_day可选号2位数字,从1开始计算,1表示1号。
opt_hours可选时2位数字,取值0~23。
opt_minutes可选分2位数字,取值0~59。
opt_seconds可选秒2位数字,取值0~59。
opt_milliseconds可选毫秒,取值0~999。
var dt = new Date(2017, 1); // 2017年2月(这里输入的月份数字为1)
console.log(dt); // 2017/2/01 00:00:00
dt = new Date(2017, 1, 25); // 2017年2月25日
console.log(dt); //2017/2/25 00:00:00
dt = new Date(2017, 1, 25, 15, 30, 40); // 2017年2月25日 15点30分40秒
console.log(dt); // 2017/2/25 15:30:40
dt = new Date(2017, 12, 25); // 2017年13月25日(这里输入的月份数字为12,表示第13个月,跳转到第二年的1月)
console.log(dt); // 2018/01/25
====实例方法
Date对象的方法分为2种形式:本地时间和UTC时间。同一个方法,一般都会有2种时间格式操作(方法名带UTC的,就是操作UTC时间),我们主要学习本地时间的操作。
2.6 get方法
getFullYear() 返回Date对象的年份值;4位年份。
getMonth() 返回Date对象的月份值。从0开始,所以真实月份=返回值+1 。
getDate() 返回Date对象的月份中的日期值;值的范围1~31 。
getHours() 返回Date对象的小时值。
getMinutes() 返回Date对象的分钟值。
getSeconds() 返回Date对象的秒数值。
getMilliseconds()返回Date对象的毫秒值。
getDay() 返回Date对象的一周中的星期值;0为星期天,1为星期一
getTime() 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
dt.getFullYear(); // 2017
dt.getMonth(); //1实际为2月份(月份从0开始计算)
dt.getDate(); // 25
dt.getHours(); // 15
dt.getMinutes(); // 36
dt.getSeconds(); // 21
dt.getMilliseconds(); // 125
dt.getDay(); //
dt.getTime(); // 返回Date对象与'1970/01/01 00:00:00'之间的毫秒值(北京时间的时区为东8区,起点时间实际为:'1970/01/01 08:00:00')
Mtch
| 属性: | |
|---|---|
| E 返回算术常量 e,即自然对数的底数(约等于2.718)。 | |
| LN2 返回 2 的自然对数(约等于0.693)。 | |
| LN10 返回 10 的自然对数(约等于2.302)。 | |
| LOG2E 返回以 2 为底的 e 的对数(约等于 1.414)。 | |
| LOG10E 返回以 10 为底的 e 的对数(约等于0.434)。 | |
| PI 返回圆周率(约等于3.14159)。 | |
| SQRT1_2 返回返回 2 的平方根的倒数(约等于 0.707)。 | |
| SQRT2 返回 2 的平方根(约等于 1.414)。 | |
| 方法: | |
| abs(x) 返回数的绝对值。 | |
| acos(x) 返回数的反余弦值。 | |
| asin(x) 返回数的反正弦值。 | |
| atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值。 | |
| atan2(y,x) 返回从 x 轴到点 (x,y) 的角度(介于 -PI/2 与 PI/2 弧度之间)。 | |
| ceil(x) 对数进行上舍入。 | |
| cos(x) 返回数的余弦。 | |
| exp(x) 返回 e 的指数。 | |
| floor(x) 对数进行下舍入。 | |
| log(x) 返回数的自然对数(底为e)。 | |
| max(x,y) 返回 x 和 y 中的最高值。 | |
| min(x,y) 返回 x 和 y 中的最低值。 | |
| pow(x,y) 返回 x 的 y 次幂。 | |
| random() 返回 0 ~ 1 之间的随机数。 | |
| round(x) 把数四舍五入为最接近的整数。 | |
| sin(x) 返回数的正弦。 | |
| sqrt(x) 返回数的平方根。 | |
| tan(x) 返回角的正切。 | |
| toSource() 返回该对象的源代码。 | |
| valueOf() 返回 Math 对象的原始值。 |
1.必学项
1.1 PI 必学 返回圆周率(约等于3.14159)不等于π
console.log(Math.PI)
var deg=Math.PI/180//定义1度
var re=36*deg
1.2 ceil(x) 必学 对数进行上舍入。 floor(x)必学 对数进行下舍入。 round(x)必学 把数四舍五入为最接近的整数。
var n=8.19
console.log(Math.ceil(n))
console.log(Math.floor(n))
console.log(Math.round(n))
全局函数也可实现
var re2=parseFloat("1239.23px123")
console.log(re2)
var re3=parseInt("1239.93px123")
console.log(re3)
数字的方法
var n=8.13
var re=n.toFixed(1)//保留一位小数
console.log(re)
var n=8.93
var re=~~n//去掉小数点 向下取整 古老代码
console.log(re)
1.3 cos(x) 必学 返回数的余弦, sin(x)必学 返回数的正弦
var deg=Math.PI/180
var re1=Math.sin(90*deg)
var re2=Math.cos(60*deg)
console.log(re1,re2)
1.4 max(x,y)必学 返回 x 和 y 中的最高值。 min(x,y)必学返回 x 和 y 中的最低值。
console.log(Math.max(10,20,60,4,1))
console.log(Math.min(10,20,60,4,1))
1.5 random()必学 返回 0 ~ 1 之间的随机数。
var re=Math.random()//伪随机数 [0,1)
console.log(re*10)//[0,10)