目录
Date
日期的对象,在JS中通过Date对象来表示一个时间
创建对象
创建一个当前的时间对象
var d = new Date();
创建一个指定的时间对象
var d = new Date("月/日/年 时:分:秒");
方法:
方法 | 描述 |
---|---|
getDate() | 当前日期对象是几日(1-31) |
getDay() | 返回当前日期对象时周几(0-6)(0为周日,1为周一) |
getMonth() | 返回当前日期对象的月份(0-11)(0为一月,1为二月) |
getFullYear() | 从 Date 对象以四位数字返回年份。 |
getHours() | 返回 Date 对象的小时 (0 ~ 23)。 |
getMinutes() | 返回 Date 对象的分钟 (0 ~ 59)。 |
getSeconds() | 返回 Date 对象的秒数 (0 ~ 59)。 |
getMilliseconds() | 返回 Date 对象的毫秒(0 ~ 999)。 |
getTime() | 返回当前日期对象的时间戳 |
Date.now() | 可以获取当前代码执行时的时间戳 |
setHours() | 设置 Date 对象中的小时 (0 ~ 23) |
Math
方法 | 描述 |
---|---|
Math.PI | 常量,圆周率 |
Math.abs() | 绝对值运算 |
Math.ceil() | 向上取整 |
Math.floor() | 向下取整 |
Math.round() | 四舍五入取整 |
Math.random() | 生成一个0-1之间的随机数 |
Math.round(Math.random()*(y-x)+x); | 生成一个x-y之间的随机数 |
Math.pow(x,y) | 求x的y次幂 |
Math.sqrt() | 对一个数进行开方 |
Math.max() | 求多个数中最大值 |
Math.min() | 求多个数中的最小值 |
字符串相关方法
方法 | 描述 |
---|---|
ES6中的字符串新方法: String.prototype.padStart(maxLength, fillString=’’) 或 String.prototype.padEnd(maxLength, fillString=’’) |
填充字符串 |
length | 获取字符串的长度 |
charAt() | 根据索引获取指定的字符 |
charCodeAt() | 根据索引获取指定的字符编码 |
String.fromCharCode() | 根据字符编码获取字符 |
indexOf() | 从一个字符串中检索指定内容(从前往后找) |
lastIndexOf() | 从一个字符串中检索指定内容(从后向前找) |
slice(start,[end]) 参数: |
从一个字符串中截取指定的内容,并将截取到内容返回,不会影响原变量 |
substr() | 和slice()基本一致,不同的是它第二个参数不是索引,而是截取的数量 |
substring() | 和slice()基本一致,不同的是它不能接受负值作为参数,如果设置一个负值,则会自动修正为0。(如果第二个参数小于第一个,自动调整位置) |
toLowerCase() | 将字符串转换为小写并返回 |
toUpperCase() | 将字符串转换为大写并返回 |