Date在创建的时候必须要创建对象。创建对象要用到关键字:new
方法:
------------------------------------------获得时间
getDate() 从Date对象返回一个月中的某一天(1-31)
getDay() 从Date对象返回一周中的某一天(0-6)
getMonth() 从Date对象返回月份(0-11)
getFullYear() 从Date对象以四位数字返回年份
getYear() 从Date对象以两位或四位数字返回年份
getHours() 返回Date对象的小时(0-23)
getMinutes() 返回对象的分钟(0-59)
getSeconds() 返回对象的秒数(0-59)
getTime() 返回现在到1970的毫秒值
toLocaleString() 使日期进行格式化
----------------------------------设置时间
setDate() 设置Date对象一个月中的某一天(1-31)
setMonth() 设置Date对象月份(0-11)
setFullYear() 设置Date对象以四位数字年份
setYear() 设置Date对象以两位或四位数字年份
setHours() 设置Date对象的小时(0-23)
setMinutes() 设置对象的分钟(0-59)
setSeconds() 设置对象的秒数(0-59)
-----------------------其他方法
toString() 把Date对象转换为字符串
案例:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>js当中搞的日期对象(Date)</title>
</head>
<body>
<script type="text/javascript">
//创建一个Date对象
var d=new Date();
document.write(d+"<br/>");//放回当前日期和时间
document.write(d.getDate()+"<br/>");//返回一个月中的日期
document.write(d.getDay()+"<br/>");//返回一个星期中的星期几
document.write(d.getFullYear()+"<br/>");//返回四个数字的年
document.write(d.getYear()+"<br/>");//返回两位数字的年,这里应该是用现在的年份减去1990
document.write(d.getMonth()+"<br/>");//返回一年中的月份
document.write(d.getHours()+"<br/>")//返回现在的小时
document.write(d.getMinutes()+"<br/>")//返回现在的分钟数
document.write(d.getSeconds()+"<br/>");//返回现在的秒钟数
document.write(d.getTime()+"<br/>");//返回现在到1970的毫秒值
document.write(d.toLocaleString()+"<br/>");//使日期格式化
</script>
</body>
</html>
Sat Apr 07 2018 20:06:38 GMT+0800 (中国标准时间)
7
6
2018
118
3
20
6
38
1523102798004
2018年4月7日 20:06:38