在js里会内置一个时间对象 ,用于操作js的时间;Date
1.获取当前时间对象
var date = new Date(); //当前的时间的时间对象;
//注意下 :这个时间是你的电脑时间,一般用的时间会从服务器获取(网络时间)
console.log(date);
2.时间对象的时间可以设置
//设置时间对象的时间方式一:
//参数分别是 年 月(注意:是从0-11月) 日 时 分 秒
//参数后面可以省略 (可以传一个 二个 三个 ....)
var date = new Date("2020","7","3");
console.log(date);//Mon Aug 03 2020 00:00:00 GMT+0800 (中国标准时间)
//设置时间对象的时间方式二:注意 月份就是 1-12月
//年份 连接符号 可以是 - / .
var date = new Date("2020-8-5 14:20:1");
console.log(date);
操作时间对象的方法
var nowDate = new Date();//大前提时间下获取以下元素
// 1.获取年份 getFullYear; 注意:都需要加小括号
var year = nowDate.getFullYear();
console.log(year);
// 2.获取月份 ; 尤其注意 月份是 0-11 也是索引
var month = nowDate.getMonth();
console.log(month+1); // 获取的月份 一定要加1 才是现在的月份
// 3. 获取天 getDate
var day = nowDate.getDate();
console.log(day);
// 4.获取星期 getDay
var day = nowDate.getDay(); //尤其要和获取天区分开
console.log(day);
// 5.获取时 : 24小时制
var hour = nowDate.getHours();
console.log(hour);
// 6.获取分 :
var minute = nowDate.getMinutes();
console.log(minute);
// 7.获取秒
var second = nowDate.getSeconds();
console.log(second);