Date对象:在js中使用Date对象来表示时间
创建Date对象
var 对象名 = new Date() //如果直接使用构造函数创建一个Date对象,则为当前代码执行时间 //如果创建一个指定时间对象,在构造函数中传递一个表示时间的字符串 格式 月/日/年 时:分:秒 <script type="text/javascript"> var d=new Date("11/11/2016 12:12:12"); console.log(d); </script>常见参数写法: 1.数字型 xx,xx,xx var d= new Date();
<script type="text/javascript"> var date1=new Date(2019,1,1,12,12,12); console.log(date1); </script>2.字符串型
<script type="text/javascript"> var date1=new Date('2019-6-7 15:23:45'); console.log(date1); </script>方法: 对象.getDate() 几日 对象.getDay() 星期几 星期天返回0 对象.getMonth() 月份 0-11应该加1 对象.getHours() 小时 对象.getMinutes() 分 对象.getSeconds() 秒 对象.getMillseconds() 毫秒 对象.getTime() 获取当前时间的时间戳,指从格林威治时间的1970年1月1日到当前时间的毫秒数(Date.now() 对象.valueOf()也可以 +new Date()最常用 ) Date.now() 获取当前时间的时间戳
<script type="text/javascript"> var d=new Date(); console.log(d.getYear()); console.log(d.getMonth()); console.log(d.getDate()); console.log(d.getDay()); console.log(d.getHours()); console.log(d.getMinutes()); console.log(d.getSeconds()); </script>