<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS对象</title>
<script>
//创建对象
/*var obj1 = new Object();
obj1.name='test';
obj1.sex='男';
var obj2 = {
name:'',
sex:''
}*/
/*Date对象:
创建对象:
1.构建系统时间
2.根据年月日创建日期对象
3.根据年月日时分秒 毫秒创建日期对象
4.根据年月日创建日期对象 格式不同
get方法:
Date没有直接访问的属性,只有获取和设置的方法
获取日期和时间的方法
getYear():返回年数;(小于2000年返回两位)
getFullYear():返回年数;
getMonth():返回当月号数;(比实际小1)
getDate():返回当日号数;
getDay():返回星期几;(0表示星期日)
getHours():返回小时数;
getMinutes():返回分钟数;
getSeconds():返回秒数;
getTime():返回毫秒数;
set方法:
设置日期和时间的方法
setYear():设置年数;
setMonth():设置当月号数;(set7表示8月)
setDate():设置当日号数;
setDay():设置星期几;
setHours():设置小时数;
setMinutes():设置分钟数;
setSeconds():设置秒数;
setTime():设置毫秒数;
*/
/*
var now = new Date();//1.构建系统时间
console.log(now);// Fri Jan 29 2021 06:53:08 GMT+0800 (中国标准时间)
var date1 = new Date(2000,01,01);//2.根据年月日创建日期对象
console.log(date1);//月份实际值加一Tue Feb 01 2000 00:00:00 GMT+0800 (中国标准时间)
var date2 = new Date(1999,04,01,10,10,10,200);//3.根据年月日时分秒 毫秒创建日期对象
console.log(date2);//Sat May 01 1999 10:10:10 GMT+0800 (中国标准时间)
var date3 = new Date('1988/06/08');//4.根据年月日创建日期对象 格式不同
console.log(date3);//月份不变 Wed Jun 08 1988 00:00:00 GMT+0900 (中国夏令时间)
console.log(date3.getYear());//获取两位 88,一般不用
console.log(now.getFullYear());//获取4位年数 ,2021
console.log(now.getMonth());//获取月份 比实际值小一, 0
console.log(now.getDate());//获取日期号数 ,29
console.log(now.getDay());//星期日返回的是0 ,5
console.log(now.getHours());//时,7
console.log(now.getMinutes());//分,9
console.log(now.getSeconds());//秒,27
console.log(now.getTime());//毫秒,1611875367601
document.write(now.getFullYear()+'年'+(now.getMonth()+1)+'月'+now.getDate()+'日 '+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds());//2021年1月29日 7:15:13
var date4 = new Date('1988/06/08');
date3.setHours(11);
date3.setMinutes(02);
date3.setSeconds(56);
console.log(date3);//Wed Jun 08 1988 11:02:56 GMT+0900 (中国夏令时间)
*/
/*
//Math对象
console.log(Math.round(5.6));//6
console.log(Math.random());//0~1之间的,0.6020288277127671
var num = Math.ceil(Math.random()*10);
console.log(num);//8
//字符串对象String
var str = 'a b c d';
var str1 = '123';
console.log(str.length);//7
console.log(str.charAt());//a
console.log(str.charCodeAt(0));//97
console.log(str.concat(str1));//a b c d123
console.log(str.indexOf('aa'));//-1
console.log(str.replace('a','A'));//A b c d
console.log(str.slice(1,3));// b
console.log(str.substring(1,3));// b
console.log(str.substr(1,3));// b ,第二个数为长度
*/
/*
//1、计算2025年的双11是星期几,距今天还有多少天,距我们现在的时间还有多少毫秒?
var date = new Date('2025/11/11');
document.write("2025年的双十一是星期"+(date.getDay()==0?'日':date.getDay()));//2025年的双十一是星期2
document.write("<br />");
var now = new Date();
document.write("2025年的双十一距离当前有"+(date.getTime()-now.getTime())+'毫秒');//2025年的双十一距离当前有150911794103毫秒
document.write("<br />");
var ss = date.getTime()-now.getTime();
var days = ss/1000/60/60/24;
document.write("2025年的双十一距离当前有"+Math.round(days)+"天");//2025年的双十一距离当前有1747天
*/
//2.定义一个函数,功能是去除字符串开头及末尾的空格
//假设输入参数为:[10个空格]abc[2空格]def[6空格]
del();//调用
function del(){
var str = " aa bb ";
//法一:
var begin;
var end;
for(var i=0;i<str.length;i++){
if(str.charAt(i) != ' '){
begin=i;
break;
}
}
for(var i=str.length-1;i>=0;i--){
if(str.charAt(i) != ' '){
end=i;
break;
}
}
str = str.substring(begin,end+1);
console.log(str);//aa bb
//法二:
console.log(str.trim());//aa bb
}
//正则表达式:用来校验一个字符串是否合法
/*
常用正则:验证邮政编码:/^\d{6}$/
验证固定电话:/^\d{3}-\d{8} | \d{4} - \d{7}$/
验证身份证号码:/^\d{15} | \d{18} | \d{17}[x X]$/
验证移动电话:/^\d13|\d15\d{9}$/
验证电子邮箱地址:/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9] ((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
* */
var reg = /^\d{6}$/;
var str = '234190';
if(str.match(reg)){
console.log('合法的邮政编码');
}else{
console.log('不合法的邮政编码');
}
</script>
</head>
<body>
</body>
</html>
JS对象
最新推荐文章于 2022-06-17 18:07:35 发布