Date 对象用于处理日期和时间
构造函数的方式
let date = new Date();
new Date() 实例化的一个日期对象
会打印出一个当前的日期
console.log(date);
通过这种方式 可以打印出 过去的时间
var tdate=new Date("9 1,2017,14:58:12");
var tdate = new Date('2019-10-30 13:20:22')
上面使用一个参数传过去的
在上面的用字符串表示的月份中 9 就代表9月 10 就代表10月
返回 Date 对象的月份,其值介于0~11之间(注:0-1月份)
用数字类型,和逗号分割的月份 是从0开始算的,这里8 就代表9月
这下面是用 6个参数 分别传过去的,所以和上面的传入的方式不一样
产生的结果也不一样
var tdate = new Date(2018,8,1,13,22,23)
console.log(tdate);
var date = new Date();
返回 Date 对象的一个月中的每一天,其值介于1~31之间
获取日期
console.log( date.getDate() );
返回 Date 对象的星期中的每一天,其值介于0~6之间(注:0代表周日)
1代表周一 2代表周二
获取星期几
console.log( date.getDay() );
返回 Date 对象的小时数,其值介于0~23之间
24点就是0点
console.log( date.getHours() );
返回 Date 对象的分钟数,其值介于0~59之间
60分就是0
console.log( date.getMinutes() );
返回 Date 对象的秒数,其值介于0~59之间
60秒就是0
console.log( date.getSeconds() );
★特殊 返回 Date 对象的月份,其值介于0~11之间(注:0代表1月份)
console.log( date.getMonth() );
返回 Date 对象的年份,其值为4位数
console.log( date.getFullYear() );