使用 Date 的构造函数
var a = new Date();
var b = new Date(Date.parse("May 17,2020"));
var c = new Date(Date.UTC(2020,5,27,17,58,37));
console.log(a);//Sat Jun 27 2020 11:06:27 GMT+0800 (中国标准时间)
console.log(b);//Sun May 17 2020 00:00:00 GMT+0800 (中国标准时间)
console.log(c);//Sun Jun 28 2020 01:58:37 GMT+0800 (中国标准时间)
使用**Date.now()**来简化Date分析代码的时间
var a = Date.now();
setTimeout(()=>{
var b = Date.now();
console.log(b-a);//3001
},3000);
console.log(a);//1593228072462
toString() 和 toLocalString() 方法
他们会根据不同浏览器不同时区格式返回表示日期的字符串。
常用的几种方式,以实例记忆
var a = new Date();
console.log(a.toDateString());//Sat Jun 27 2020
console.log(a.toTimeString());//11:36:51 GMT+0800 (中国标准时间)
console.log(a.toLocaleDateString());//2020/6/27
console.log(a.toLocaleTimeString());//上午11:38:36
console.log(a.toUTCString());//Sat, 27 Jun 2020 03:39:31 GMT
编写一个倒计时
var a = new Date("2020/9/1 08:35:37");
var end = a.getTime();
function sumTime() {
var last = Date.now();
var days, dayRm, hours, hourRm, mins, minRm, secs;
days = (end-last)/(3600*1000*24);
dayRm = (end-last)%(3600*1000*24);
hours = dayRm/(3600*1000);
hourRm = dayRm%(3600*1000);
mins = hourRm/(60*1000);
minRm = hourRm%(60*1000);
secs = minRm/(1000);
console.log(parseInt(days) + ' 天 ' + parseInt(hours) + ' 时 ' + parseInt(mins) + ' 分 ' + parseInt(secs) +' 秒 ');
}
console.log('距开学还有\n');
setInterval(()=> {
sumTime();
},1000);
正则表达式,可使用字面量也可以使用构造函数。不一样的是字面量始终是共享一个RegExp实例,而构造函数创建的每一个都是新实例,但是当重复进行相同的查找时就会出现问题,所以ES5之后规定,字面量模式也要像构造函数一样创建新的实例
/ pattern / flags
var reg = /great/g;
var regs = new RegExp("great","g");
紧跟表达式后的是匹配模式标志
这三个属性可以测试模式 pattern 是否是对应模式 例如pattern.global
表示开始搜索的下一个匹配项字符位置,从0开始
符号 -,在方括号中使用,指定范围,n~m,[n-m],一般指数字和字母的范围
规定指定的字符出现的次数
- 重复1或者更多次,* 重复0或者更多次,? 重复0或1次
$、(、)、*、+、.、[、]、?、\、/、^、{、}、|
这一类字符不转义就会被当做各种限定、连接、定位符处理
接受一个参数,即要搜索匹配的字符串,该方法返回一个Array实例,该实例包括两个属性 index 和 input,index 表示匹配项在字符串中的位置,input 表示正则表达式应用到的字符串。
var reg = /.+\.(jpg|png|text)/g;
var result = reg.exec("great.png");
console.log(result.index);//0
console.log(result[0])//great.png
console.log(result.lastIndex);//undefined
接受一个字符串参数,模式与其匹配返回 true,否则为 false
var reg = /.+\.(jpg|png|text)/g;
var result = reg.test("great.png");
console.log(result);//true
opera不支持 input,lastMatch,lastParen,multiline Internet Explorer不支持multiline
var phone = /1[0-9]{10}/g
console.log(phone.test("13638707775"));//true
console.log(phone.test("23638707775"));//false
console.log(phone.test("1363870"));//false
var email = /[a-zA-Z0-9_]+@[0-9a-zA-Z]+\.(com|net)/g //邮箱登录名支持字母、数字、下划线
console.log(email.test("grea_12tiga@126.com"));//true
console.log(email.test("app@123.net"));//true
console.log(email.test("app%@123.net"));//false
console.log(email.test("app@123.54"));//false
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有