内置对象Date
<script>
var now = new Date();
// console.log(now);
var y=now.getFullYear();//获取年份
var m=now.getMonth()+1;//获取月份 月份从0开始 0表示1月
var day=now.getDate();
var week=now.getDay();//获取当前是周几 周日对应0
var h=now.getHours();//小时数
var min=now.getMinutes();
var s=now.getSeconds();
var ms=now.getUTCMilliseconds();//毫秒
var time=now.getTime();//获取从1970年1月1日凌晨到现在的秒数
//与获取日期相对应 方法有set方法 确定日期
console.log(y,m,day,week,h+":"+min+":"+s)
console.log(time);
var that = new Date("2020-4-28");//实例化date时可以传参(指定日期)注意!!以2020-4-28形式传参时月份不用加1
var week1 = that.getDay();
console.log(week1);
//下个月1号是周几?
var a=new Date("2020-5-1");
var week2=a.getDay();
console.log(week2);
//计算5天后是几月几号?
var date=new Date();
date.setDate(now.getDate() + 5);
console.log((date.getMonth()+1)+"-"+date.getDate());
//在小于10的日期前面加个0
// var date2=new Date("2020-5-3");
if(m<10){
m ="0"+m;
}if(day<10){
day="0"+day
}
var week3 =["周日","周一","周二","周三","周四","周五","周六"];
console.log(`${y}-${m}-${day} ${week3[week]}`);//模板字符串
</script>