一. 用new 构造函数创建对象
<script>
1. var date=new Date();
console.log(date);
2. var date1=new Date('2022-9-5 13:43:30');
console.log(date1);
console.log(date1.getFullYear());
console.log(date1.getMonth());
console.log(date1.getHours());
console.log(date1.getMinutes());
var time=new Date().getTime();
console.log(time);
</script>
当Date()里没有任何参数时,默认为获取当前的时间,如1
当Date()里有参数时,相当于将设置的这个时间给了date1,如2
ps日期格式化
就是自己设置获取的年月日时分秒等的格式
二. 获取年月日时分秒
getFullyear()
getmonth()等
三. 时间戳
相当于是获取从标准时间---->格林威治标准时间的1970年1月1日,0时0分0秒(北京时间1970年01月01日08时00分00秒)起,到现在这一时刻的总的秒数
用 valueOf() getTime()方法获得时间戳
var time=new Date().getTime();
console.log(time);
四种方法:
var time1=new Date().getTime();
console.log(time1);
var time2=new Date();
console.log(time2.valueOf());
var time3=+new Date();
console.log(time3); 最常用的写法
第四种最简单的方法 不用new
console.log(Date.now()); 直接就可以输出
最后
以上就是天真河马最近收集整理的关于日期函数Date的全部内容,更多相关日期函数Date内容请搜索靠谱客的其他文章。
发表评论 取消回复