八、Date对象
(一)Date 概述
- Date对象和Math对象不一样,他是一个构造函数,所以我们需要实例化后才能使用
- Date实例用来处理日期和时间
(二)Date()方法的使用
1.获取当前时间必须实例化
var now = new Date();
console.log(now)//当前时间
2.Date()构造函数的参数
如果括号里面有时间,就返回参数里面的时间。
例如日期格式字符串为’2021-12-29‘,可以写成new Date(‘2021-12-29’)或者 new Date(‘2021/12/29’)
如果Date()不写参数,就返回当前时间
如果Date()里面写参数,就返回括号里面输入的时间
案例
// 1. 使用Date 如果没有参数 返回当前系统的当前时间
var date = new Date();
console.log(date);
// 2. 参数常用的写法 数字型 2021, 10,29 或者是 字符串型 '2021-10-29 8:8:8'
var date1 = new Date(2021, 10, 29);
console.log(date1); // 返回的是 11月 不是 10月
var date2 = new Date('2021-10-29 8:8:8');
console.log(date2);
3.日期格式
方法名 | 说明 |
---|---|
getFullYear() | 获取当年 |
getMonth() | 获取当月(0一11) |
getDate() | 获取当天日期 |
getDay() | 获取星期几(周日0到周六6) |
getHours() | 获取当前小时 |
getMinutes() | 获取当前分钟 |
getSeconds() | 获取当前秒钟 |
案例
// 格式化日期 年月日
var date = new Date();
console.log(date.getFullYear()); // 年
console.log(date.getMonth() + 1); // 月(返回的月份小1个月 记得月份+1 呦)
console.log(date.getDate()); // 日
console.log(date.getDay()); // 周几(周一返回的是 1 周六返回的是 6 但是 周日返回的是 0)
console.log(date.getHours()); // 时
console.log(date.getMinutes()); // 分
console.log(date.getSeconds()); // 秒
var year = date.getFullYear();
year = year < 10 ? "0" + year : year;
var month = date.getMonth() + 1;
var month = month < 10 ? "0" + month : month;
var dates = date.getDate();
dates = dates < 10 ? "0" + dates : dates;
var h = date.getHours();
h = h < 10 ? "0" + h : h;
var m = date.getMinutes();
m = m < 10 ? "0" + m : m;
var s = date.getSeconds();
s = s < 10 ? "0" + s : s;
var day = date.getDay();
var arr = [
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六",
];
console.log(
"今天是:" +
year +
"年" +
month +
"月" +
dates +
"日 " +
h +
"时" +
m +
"分" +
s +
"秒" +
arr[day]
);//xxxx年xx月xx日 xx时xx秒xx日 星期x
4.获取日期的总的毫秒形式
- Date对象是基于1970年1月1日(世界标准时间)起的毫秒数
- 我们经常利用总的毫秒数来计算时间,因为它更精确
//实例化Date对象
var date = new Date();
// 1.用于获取对象的原始值
console.log(date.valueOf());
console.log(date.getTime());
// 2.简单写可以这么做
var now = +new Date();
console.log(now);
// 3. HTML5中提供的方法,有兼容性问题
var now = Date.now();
console.log(now);
- 以毫秒形式转换时间
d = parselnt(总秒数/ 60/60/24); 〃计算天数
h = parselnt(总秒数/ 60/60 %24) // 计算小时
m = parselnt(总秒数/60%60); // 计算分数
s = parselnt(总秒数%60); //计算当前秒数
案例
//2024-09-26 11:50:00
let str = prompt("请输入结束时间如(YYY-MM-DD HH:MM:SS):");
function countdown() {
let times = +new Date();
let endTimes = +new Date(str);
if (endTimes < times) {
alert("输入的时间不能是过去的时间!");
clearInterval(interval); // 清除定时器
return;
}
timeDifference = (endTimes - times) / 1000;
let years = parseInt(timeDifference / 60 / 60 / 24 / 365);
let days = parseInt(timeDifference / 60 / 60 / 24);
let hours = parseInt((timeDifference / 60 / 60) % 24);
let minutes = parseInt((timeDifference / 60) % 60);
let seconds = parseInt(timeDifference % 60);
years = years < 10 ? "0" + years : years;
days = days < 10 ? "0" + days : days;
hours = hours < 10 ? "0" + hours : hours;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
console.log(`${years}年${days}天${hours}时${minutes}分${seconds}秒`);
}
countdown();
setInterval(function () {
countdown();
}, 1000);