创建一个Date对象
概念: Date对象是用来处理时间和日期,内置了一系列获取和设置日期和时间的方法
使用:
1.每次在使用它之前,都需要配合new关键字, 来生成一个Date对象,
2.然后通过Date对象,再去调用各种方法
备注: Date获取的是当前计算机的本地时间
var date = new Date();
console.log(date);
toLocaleString(): 可把时间对象 转换为字符串(我们需要的格式) console.log(date.toLocaleString());
getTime(): 返回的是 1970年1月1日距今的 毫秒数
console.log(date.getTime());
------------------------------设置 时间和日期------------------------------------------------------
setTime(): 以毫秒数 设置Date对象
date.setTime(93599945322);
console.log(date.toLocaleString());
setFullYear(): 设置 年 月 日
备注: 月份 取值 0 ~ 11
date.setFullYear(2019,7,24);
console.log(date.toLocaleString());
setMonth() : 设置 月 日 毫秒
date.setMonth(7,24);
console.log(date.toLocaleString());
setHours() : 设置 时 分 秒 毫秒
date.setHours(12,0,0);
console.log(date.toLocaleString());
setMinutes(): 分 秒 毫秒
date.setMinutes();
-------------------------------------获取时间和日期-------------------------------------
console.log(date.getFullYear()); // 获取年份
console.log(date.getMonth()); // 获取到月份 取值范围 0~11
console.log(date.getDate()); // 获取一个月中的第几天 console.log(date.getDay()); // 获取一周中的第几天 console.log(date.getHours()); // 获取小时 console.log(date.getMinutes()); // 获取分钟 console.log(date.getSeconds()); // 获取秒数