1.随机数
1.Math.random() 随机声明0-1之间的数 包括0不包括1
2.随机整数的公式:公式:Math.random * (max-min+1) + min
2.Date时间对象
2.1创建时间对象
创建当前时间
1.创建现在这一时刻 当前计算机这一秒的时间
2.打印输出每秒的时间
创建任意时间
方法1:
new Date(年,月-1,日,时,分,秒) 2021年10月1日 12:10:00
方法2:
new Date("年,月,日,时:分:秒");
new Date("年-月-日 时:分:秒");
new Date("年/月/日 时:分:秒");
new Date("年 月 日 时:分:秒");
方法3:
var c = new Date("Oct 01 2021");
2.2获取时间
获取特定格式的时间
获取特定格式的时间 (年月日)
console.log(yDate.toDateString());//Fri Oct 21 2022
console.log(yDate.toLocaleDateString());//2022/10/21
获取特定格式的时间 (时分秒)
console.log(yDate.toTimeString());//10:47:44 GMT+0800 (中国标准时间)
console.log(yDate.toLocaleTimeString());//10:48:08
获取单个时间
获取单个时间 Number数据类型
console.log(yDate.getFullYear())
console.log(yDate.getMonth())
console.log(yDate.getDate());
console.log(yDate.getHours());
console.log(yDate.getMinutes())
console.log(yDate.getSeconds());
时间戳:时间的毫秒单位 从1970年1月1日到现在时刻的毫秒
console.log(yDate.getTime());//1666320938256
获取2023年10月1日的时间戳
var xDate = new Date(2023, 9, 1);
console.log(xDate.getTime());//1696089600000
2.3倒计时
3.String字符串对象
3.1创建字符串
1.字面量创建(包装类对象,但是在使用字符串对象的方法的时候,会暂时伪装成一个对象)
2.new关键字创建
3.2字符串的方法
length
获取字符串的长度
charAt
charAt(下标):获取字符串对应下标的字符
charCodeAt(下标):获取字符串对应下标的字符编码 "0"-48 "a"-97 "A"-65
indexOf
indexof(searchValue,index)
作用:查询一个字符串在另一个字符串中首次出现的位置,并且返回首次出现位置的下标,如果找不到则是返回-1
参数
searchValue:必需参数,需要查询的字符串
index:可选参数,开始查找的位置 如果不指定index 默认是0
lastIndexOf
lastIndexof(searchValue,index)
作用:查询一个字符串,从后往前查找最后一次出现的位置,并且返回最后一次位置的下标,如果找不到则是返回-1;
参数
searchValue:必需参数,需要查询的字符串
index:可选参数,开始查找的位置 如果不指定index 默认是str.length-1
字符串的截取方法
1.substring
substring(start,end)
作用:截取字符串中一串连续的字符串,包括开始下标,不包含结束下标
参数
start:必需参数,开始截取的下标位置
end:可选参数,结束截取的下标位置 如果不写默认是str.length
2.slice
slice(start,end)
作用:截取字符串中一串连续的字符串,包括开始下标,不包含结束下标
参数
start:必需参数,开始截取的下标位置
end:可选参数,结束截取的下标位置 如果不写默认是str.length
3.substring和substr的区别?
substring会自动调整下标的位置 slice不会
substring碰见负数会默认为0 slice碰见负数会认为是倒数第几个
4.substr
substr(start,count)
作用:从开始下标开始截取,截取几个
1.大小写转换
toLowerCase 转小写
toUpperCase 转大写
使用场景:验证码
2.replace
replace(searchStr,replaceStr)
作用:替换字符串中的某个字符,返回一个新的字符串,==默认一次只能替换一个==
参数
searchStr:必需参数,将要被替换的字符
replaceStr:必需参数,替换的字符
3.trim
去除字符串的首尾空格
4.split
split(分割标识):将字符串根据分割标识进行分割 并且组成一个新的数组